我如何遍历一个IGrouping< T>接口? [英] How do I iterate an IGrouping<T> Interface?
问题描述
Ive得到了部份非常恼人的问题,我已经分组的一组数据,我不能让该组内的数据。我能到的键位而不是数据。
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..
我有数据的负载是形式
Data.Period = x
Data.Adjustment = y
我执行GROUPBY
I perform a GroupBy
var groupedData = Data.GroupBy(x => x.Period new {x.Period});
这带回(根据LINQ)我是开始寻找我多么希望它的分层数据。我还想通过的内容,但不能遍历......我该怎么办呢?和我怎么...
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...
工作如何才能到Data.Adjustment不使用投影。而且我不希望因为我必须执行另一组....帮助项目呢! : - )
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! :-)
推荐答案
的 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));
这篇关于我如何遍历一个IGrouping< T>接口?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!