C# 扩展方法获取";方法“无过载”;错误
我最近刚刚将这个项目从ASP.NET3.5升级到4.0,这样我就可以使用concurrentDictionary而不是Dictionary,因为它具有线程安全功能 为了使用它,我使用帮助论坛中的代码创建了一个扩展 这一切都非常接近工作,我不知道如何修改扩展,使其正常工作 代码如下:C# 扩展方法获取";方法“无过载”;错误,c#,asp.net,extension-methods,concurrentdictionary,C#,Asp.net,Extension Methods,Concurrentdictionary,我最近刚刚将这个项目从ASP.NET3.5升级到4.0,这样我就可以使用concurrentDictionary而不是Dictionary,因为它具有线程安全功能 为了使用它,我使用帮助论坛中的代码创建了一个扩展 这一切都非常接近工作,我不知道如何修改扩展,使其正常工作 代码如下: var catalogs = (from _catalog in entities.catalogs from rolePermission in entities.c
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));
}
公共静态类扩展
{
公共静态ConcurrentDictionary到ConcurrentDictionary(
这是(不可数的来源)
{
返回新的ConcurrentDictionary(源);
}
公共静态ConcurrentDictionary到ConcurrentDictionary(
此IEnumerable源,Func键选择器)
{
返回新的ConcurrentDictionary(
从v源
选择新的KeyValuePair(键选择器(v),v));
}
这就是我收到的错误:
错误1方法“ToConcurrentDictionary”没有重载包含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);
}
公共静态ConcurrentDictionary到ConcurrentDictionary(此IEnumerable源、Func键选择器、Func值选择器)
{
var pairs=source.Select(i=>newkeyvaluepair(keySelector(i)、valueSelector(i));
返回新的ConcurrentDictionary(对);
}
您没有可以从项目中提取值的重载:
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);
}
公共静态ConcurrentDictionary到ConcurrentDictionary(此IEnumerable源、Func键选择器、Func值选择器)
{
var pairs=source.Select(i=>newkeyvaluepair(keySelector(i)、valueSelector(i));
返回新的ConcurrentDictionary(对);
}
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));
}
公共静态ConcurrentDictionary到ConcurrentDictionary(
此IEnumerable源、表达式键选择器)
{
返回新的ConcurrentDictionary(
从v源
选择新的KeyValuePair(键选择器(v),v));
}
没有代码保证,但我建议阅读,而from MSDNFunc关键字实际上更像是一个返回值的函数。您可能会看到一个“表达式”来传递此类内容。类似于此:
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));
}
公共静态ConcurrentDictionary到ConcurrentDictionary(
此IEnumerable源、表达式键选择器)
{
返回新的ConcurrentDictionary(
从v源
选择新的KeyValuePair(键选择器(v),v));
}
没有代码保证,但我建议阅读,而from MSDN是一种不需要中间转换为
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;
}
}
公共静态类ConcurrentDictionaryExtensions
{
公共静态ConcurrentDictionary到ConcurrentDictionary(
这是一个数不清的来源,
Func键选择器,
Func元素选择器)
{
返回OnCurrentDictionary(source、keySelector、elementSelector、EqualityComparer.Default);
}
公共静态ConcurrentDictionary到ConcurrentDictionary(
这是一个数不清的来源,
Func键选择器,
Func元素选择器,
IEqualityComparer(键盘比较器)
{
if(source==null)
抛出新的ArgumentNullException(“源”);
if(keySelector==null)
抛出新ArgumentNullException(“KeySelect”);
if(elementSelector==null)
抛出新ArgumentNullException(“elementSelector”);
ConcurrentDictionary dest=新的ConcurrentDictionary(keyComparer);
foreach(源中的TSource条目)
{
var键=键选择器(输入);
变量元素=元素选择器(条目);
目的地址更新(键,元素,(k,e)=>元素);
}
返回目的地;
}
}
一种不需要中间转换为键值对的变体,它与Linq的到字典的实现相匹配
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;
}
}
公共静态类ConcurrentDictionaryExtensions
{
公共静态ConcurrentDictionary到ConcurrentDictionary(
这是一个数不清的来源,
Func键选择器,
Func元素选择器)
{
返回OnCurrentDictionary(source、keySelector、elementSelector、EqualityComparer.Default);
}
公共静态ConcurrentDictionary到ConcurrentDictionary(
这是一个数不清的来源,
Func键选择器,
Func元素选择器,
IEqualityComparer(键盘比较器)
{
if(source==null)
抛出新的ArgumentNullException(“源”);
if(keySelector==null)
抛出新ArgumentNullException(“KeySelect”);
if(elementSelector==null)
抛出新ArgumentNullException(“elementSelector”);
ConcurrentDictionary dest=新的ConcurrentDictionary(keyComparer);
foreach(源中的TSource条目)
{
var键=键选择器(输入);
变量元素=元素选择器(条目);
目的地址更新(键,元素,(k,e)=>元素);
}
返回目的地;
}
}
这非常有效,非常感谢Lee。现在我将尝试了解更多信息,以便了解其工作原理。这非常有效,非常感谢Lee。现在我将尝试了解更多信息,以便了解其工作原理。