本文介绍了扩展方法获取"对于方法&QUOT没有超载;错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

要使用它,我用帮助论坛中发现code创建一个扩展。

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.

下面是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());

而code的扩展:

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:

错误1无过载方法'ToConcurrentDictionary需要两个参数

我需要修改扩展到在这种情况下工作?任何建议都大大AP preciated。

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

推荐答案

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

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

这篇关于扩展方法获取&QUOT;对于方法&QUOT没有超载;错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 00:07