gpt4 book ai didi

c# - 循环 ILookup,访问值

转载 作者:可可西里 更新时间:2023-11-01 08:56:28 26 4
gpt4 key购买 nike

我有一个 ILookup< string, List<CustomObject> >从我做过的一些 linq。

我现在想遍历结果:

foreach(IGrouping<string, List<CustomObject>> groupItem in lookupTable)
{
groupItem.Key; //You can access the key, but not the list of CustomObject
}

我知道我一定是在歪曲 IGrouping作为 KeyValuePair ,但现在我不确定如何正确访问它。

最佳答案

ILookup是列表的列表:

public interface ILookup<TKey, TElement> : IEnumerable<IGrouping<TKey, TElement>>

所以因为IGrouping<TKey, TElement>是(实现)...

IEnumerable<TElement>

...查找是

IEnumerable<IEnumerable<TElement>>

在你的情况下 TElement 也是一个列表,所以你最终得到

IEnumerable<IEnumerable<List<CustomObject>>>

这就是您如何遍历客户:

foreach(IGrouping<string, List<CustomObject>> groupItem in lookupTable)
{
groupItem.Key;
// groupItem is <IEnumerable<List<CustomObject>>
var customers = groupItem.SelectMany(item => item);
}

关于c# - 循环 ILookup,访问值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24099061/

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