C# 如何迭代 IGrouping<T> 接口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9723627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I iterate an IGrouping<T> Interface?
提问by Exitos
Ive got ths really annoying issue I have grouped a set of data and I cant get to the data within the group. I can get to the key bit not the data..
我遇到了非常烦人的问题,我将一组数据分组,但无法访问组内的数据。我可以得到关键位而不是数据..
I have a load of data that is in the form
我有大量的数据形式
Data.Period = x
Data.Adjustment = y
I perform a GroupBy
我执行一个 GroupBy
var groupedData = Data.GroupBy(x => x.Period new {x.Period});
This brings back (according to linq) my hierarchical data which is starting to look how I want it. Id like to iterate through the contents but cant...How do I do this? and how do I...
这带回(根据 linq)我的分层数据,它开始看起来我想要它。我想遍历内容但不能......我该怎么做?还有我怎么...
Work how to get to the Data.Adjustment without using a projection. And I dont want to project yet because I have to perform another grouping....help! :-)
了解如何在不使用投影的情况下访问 Data.Adjustment。而且我还不想进行投影,因为我必须执行另一个分组....帮助!:-)
采纳答案by phoog
The IGrouping<TKey, TElement>interface inherits IEnumerable<TElement>:
该IGrouping<TKey, TElement>接口继承IEnumerable<TElement>:
foreach (var group in groupedData)
{
var groupKey = group.Key;
foreach (var groupedItem in group)
DoSomethingWith(groupKey, groupedItem);
}
I note that you will be better off using this for your query, however:
我注意到,您最好将它用于您的查询,但是:
var groupedData = Data.GroupBy(x => x.Period);
rather than this:
而不是这个:
var groupedData = Data.GroupBy(x => new {x.Period});
If, for example, you wanted to average the adjustments, you could do this:
例如,如果您想平均调整,您可以这样做:
foreach (var group in groupedData)
Console.WriteLine("Period: {0}; average adjustment: {1}", group.Key, group.Average(i => i.Adjustment));
回答by BrokenGlass
Just use foreach:
只需使用foreach:
foreach(var group in groupedData)
{
Console.WriteLine("Period: {0}", group.Key);
foreach(var item in group)
Console.WriteLine(" Adjustment: {0}", item.Adjustment);
}
回答by jason
Each element of a sequence of IGrouping<TKey, TElement>is an IEnumerable<TElement>that you can iterate over to get the data that has a common TKey:
的序列中的每个元素IGrouping<TKey, TElement>都是IEnumerable<TElement>,您可以迭代它以获取具有公共 的数据TKey:
var groups = Data.GroupBy(x => x.Period);
foreach(var group in groups) {
Console.WriteLine("Period: {0}", group.Key);
foreach(var item in group) {
Console.WriteLine("Adjustment: {0}", item.Adjustment);
}
}
So in the above, groupsis an IEnumerable<IGrouping<TPeriod, TAdjustment>>where TPeriodis the type of Period(you didn't tell us) and TAdjustmentis the type of Adjustment. Then, groupis an object that implements IEnumerable<TAdjustment>(but it also has a Keyproperty so that you can get the key. Finally, itemis a TAdjustment, and for each group, all the itemthat come from iterating over that grouphave the same key.
所以在上面,groups是IEnumerable<IGrouping<TPeriod, TAdjustment>>whereTPeriod是Period(你没有告诉我们)TAdjustment的类型,是Adjustment. 然后,group是一个实现的对象IEnumerable<TAdjustment>(但它也有一个Key属性,以便您可以获取密钥。最后,item是 a TAdjustment,对于每个group,所有item来自迭代的group具有相同密钥的。
回答by everGreen
Though maybe obvious for others one can also use:
尽管对其他人来说可能很明显,但也可以使用:
var groups = Data.GroupBy(x => x.Period);
foreach(var group in groups)
{
List<Data> dataListByPeriod = group.ToList();
//use this list
}
回答by Isuru Chandrasiri
You can use following code as well.
您也可以使用以下代码。
var groupedData = Data.GroupBy(x => x.Period);
foreach (var group in groupedData) {
var gr = group.FirstOrDefault();
Console.WriteLine("Period: {0}", gr.Period);
Console.WriteLine("Adjustment: {0}", gr.Adjustment);
}

