【问题标题】:Extension method Gets "No overload for method" Error扩展方法获取“方法没有重载”错误
【发布时间】:2012-09-24 16:44:39
【问题描述】:

我最近刚刚将这个项目从 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 个参数

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

【问题讨论】:

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


    【解决方案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】:

      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

      【讨论】:

        【解决方案3】:

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

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

        【讨论】:

        • 这很好,非常感谢李。现在我将尝试了解更多,以便知道它为什么有效。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-02-13
        相关资源
        最近更新 更多