Extension method Gets “No overload for method” Err

2019-09-15 05:32发布

问题:

I just recently upgraded this project from ASP.Net 3.5 to 4.0 so that I could use the concurrentDictionary instead of Dictionary because of the thread safe feature.

To use it I created an extension using code found in help forums.

It is all very close to working and I don't know how I can modify the extension for it to work properly.

Here is the code:

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

And the code in the extension:

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

And this is the error that I receive:

Error 1 No overload for method 'ToConcurrentDictionary' takes 2 arguments

What would I need to modify for the extension to work in this situation? Any suggestions are greatly appreciated.

回答1:

You don't have an overload which allows you to extract the value from an item:

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


回答2:

A variant that does not require an intermediate conversion to a KeyValue pair, and matches Linq's implementation of 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;
    }
}


回答3:

The Func keyword is actually more like a function that returns a value. You might be looking at an "Expression" to pass in that kind of stuff. Something close to this:

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

No code guarentee, but I would suggests reading THIS POST, and the Expression Class from MSDN