gpt4 book ai didi

c# - IEnumerable.ToLookup

转载 作者:行者123 更新时间:2023-12-04 12:26:48 24 4
gpt4 key购买 nike

我正在尝试转 IEnumerable<KeyValuePair<string, object>>进入 ILookup<string, object>使用以下代码:

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

但是编译器提示:

Instance argument: cannot convert from 'System.Collections.Generic.List>' to 'System.Collections.Generic.IEnumerable'

'System.Collections.Generic.List>' does not contain a definition for 'ToLookup' and the best extension method overload 'System.Linq.Enumerable.ToLookup(System.Collections.Generic.IEnumerable, System.Func)' has some invalid arguments

cannot convert from 'lambda expression' to 'System.Func'

我对 lambda 表达式做错了什么?

最佳答案

只需删除 <string, object>对于自动推断的类型:

var lookup = list.ToLookup(a => a.Key);

它真的应该是:

var lookup = list.ToLookup<KeyValuePair<string, object>, string>(a => a.Key);

关于c# - IEnumerable<T>.ToLookup<TKey, TValue>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13597742/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com