Extension方法获取“方法没有重载”错误

时间:2012-10-03 17:36:21

标签: c# asp.net extension-methods concurrentdictionary

我刚刚将这个项目从ASP.Net 3.5升级到4.0,因此我可以使用concurrentDictionary而不是Dictionary,因为它具有线程安全功能。

要使用它,我使用帮助论坛中的代码创建了扩展程序。

这一切都非常接近于工作,我不知道如何修改扩展以使其正常工作。

以下是代码:

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'没有重载需要2个参数

在这种情况下,我需要修改哪个扩展才能工作?非常感谢任何建议。

3 个答案:

答案 0 :(得分:2)

您没有允许您从项目中提取值的重载:

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);
}

答案 1 :(得分:1)

不需要中间转换为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;
    }
}

答案 2 :(得分:0)

Func关键字实际上更像是一个返回值的函数。你可能正在看一个“表达”来传递那种东西。接近这一点:

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));     
}

没有代码保护,但我建议您阅读THIS POST,以及来自MSDN的Expression Class