C# IEnumerable<;T>;。托卢库普<;TKey,TValue>;
我正在尝试使用以下代码将C# IEnumerable<;T>;。托卢库普<;TKey,TValue>;,c#,linq,lambda,lookup,C#,Linq,Lambda,Lookup,我正在尝试使用以下代码将IEnumerable转换为ILookup: var list = new List<KeyValuePair<string, object>>() { new KeyValuePair<string, object>("London", null), new KeyValuePair<string, object>("London", null), new KeyValuePair<strin
IEnumerable
转换为ILookup
:
var list = new List<KeyValuePair<string, object>>()
{
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("London", null),
new KeyValuePair<string, object>("Sydney", null)
};
var lookup = list.ToLookup<string, object>(a => a.Key);
var list=新列表()
{
新的KeyValuePair(“伦敦”,空),
新的KeyValuePair(“伦敦”,空),
新的KeyValuePair(“伦敦”,空),
新的KeyValuePair(“Sydney”,null)
};
var lookup=list.ToLookup(a=>a.Key);
但编译器抱怨:
实例参数:无法从转换
“System.Collections.Generic.List>”
到
'System.Collections.Generic.IEnumerable'
及
“System.Collections.Generic.List>”
不包含“ToLookup”和最佳扩展的定义
方法重载
'System.Linq.Enumerable.ToLookup(System.Collections.Generic.IEnumerable,
System.Func“”具有一些无效参数
及
无法从“lambda表达式”转换为“System.Func”
lambda表达式有什么问题吗?只需删除
即可自动推断类型:
var lookup = list.ToLookup(a => a.Key);
实际上应该是:
var lookup = list.ToLookup<KeyValuePair<string, object>, string>(a => a.Key);
var lookup=list.ToLookup(a=>a.Key);
Aha!谢谢,我从文档中没有意识到第一个参数是源类型