0

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

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

4

3 回答 3

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);
}
于 2012-10-03T21:47:48.893 回答
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;
    }
}
于 2013-08-31T20:17:21.177 回答
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

于 2012-10-03T21:54:35.540 回答