扩展方法获取“不超载的方法”错误(Extension method Gets “No overloa

2019-11-01 03:31发布

我最近刚刚从ASP.Net 3.5升级这个项目到4.0,这样我可以使用的,因为线程安全功能的concurrentDictionary而不是字典。

要使用它,我创建使用帮助论坛中发现的代码的扩展。

现在的情况很接近的工作,我不知道我怎么可以修改扩展它才能正常工作。

下面是代码:

var catalogs = (from _catalog in entities.catalogs
                        from rolePermission in entities.c_roleperm
                        from _group in entities.c_group
                        from _user in entities.c_user
                        where _group.id == rolePermission.groupID
                            && rolePermission.roleID == user.roleID
                            && _catalog.groupID == rolePermission.groupID
                            && _user.id == _catalog.userID
                        select new { name = _catalog.name, groupID = _catalog.groupID, userName = _user.name, userID = _catalog.userID, groupName = _group.name, ID = _catalog.id }
                );


var listItems = catalogs.ToList(p => new CatalogItem() { name = p.name, groupID = p.groupID, userID = p.userID, username = p.userName, groupName = p.groupName, ID = p.ID }).GroupBy(p => p.groupName).ToConcurrentDictionary(p => p.Key, p => p.ToList());

而在扩展的代码:

public static class Extentions
{
    public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(
          this IEnumerable<KeyValuePair<TKey, TValue>> source)
    {
        return new ConcurrentDictionary<TKey, TValue>(source);
    }
    public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>(
           this IEnumerable<TValue> source, Func<TValue, TKey> keySelector)
    {
        return new ConcurrentDictionary<TKey, TValue>(
            from v in source
            select new KeyValuePair<TKey, TValue>(keySelector(v), v));
    }

这是我收到的错误:

错误1无重载方法“ToConcurrentDictionary”需要两个参数

什么会我需要修改扩展到在这种情况下工作吗? 任何建议都非常赞赏。

Answer 1:

您没有过载,它允许你从项目中提取的价值:

public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<T, TKey, TValue>(this IEnumerable<T> source, Func<T, TKey> keySelector, Func<T, TValue> valueSelector)
{
    var pairs = source.Select(i => new KeyValuePair<TKey, TValue>(keySelector(i), valueSelector(i)));
    return new ConcurrentDictionary<TKey, TValue>(pairs);
}


Answer 2:

不需要中间转换为变体KeyValue对,和LINQ的实现的匹配ToDictionary

public static class ConcurrentDictionaryExtensions
{
    public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TKey, TSource, TElement>(
        this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector,
        Func<TSource, TElement> elementSelector)
    {
        return ToConcurrentDictionary(source, keySelector, elementSelector, EqualityComparer<TKey>.Default);
    }

    public static ConcurrentDictionary<TKey, TElement> ToConcurrentDictionary<TKey, TSource, TElement>(
        this IEnumerable<TSource> source,
        Func<TSource, TKey> keySelector,
        Func<TSource, TElement> elementSelector,
        IEqualityComparer<TKey> keyComparer)
    {
        if (source == null)
            throw new ArgumentNullException("source");
        if (keySelector == null)
            throw new ArgumentNullException("keySelector");
        if (elementSelector == null)
            throw new ArgumentNullException("elementSelector");

        ConcurrentDictionary<TKey, TElement> dest = new ConcurrentDictionary<TKey, TElement>(keyComparer);
        foreach (TSource entry in source)
        {
            var key = keySelector(entry);
            var element = elementSelector(entry);
            dest.AddOrUpdate(key, element, (k, e) => element);
        }
        return dest;
    }
}


Answer 3:

该函数功能的关键字其实更像是一个返回值的函数。 你可能会寻找一个“表达”的那种东西通过。 一些接近这一点:

public static ConcurrentDictionary<TKey, TValue> ToConcurrentDictionary<TKey, TValue>  (
    this IEnumerable<TValue> source, Expression<Func<T, bool>> keySelector)     
{         
return new ConcurrentDictionary<TKey, TValue>(
         from v in source
         select new KeyValuePair<TKey, TValue>(keySelector(v), v));     
}

无码机制保障,但我会建议阅读这篇文章 ,并表达类从MSDN



文章来源: Extension method Gets “No overload for method” Error