[英]How do I iterate an IGrouping<T> Interface?
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. 这带回(根据linq)我的分层数据,它开始看起来我想要它。 Id like to iterate through the contents but cant...How do I do this?
我喜欢迭代内容但不能......我该怎么做? and how do I...
我怎么...
Work how to get to the Data.Adjustment without using a projection. 了解如何在不使用投影的情况下进入Data.Adjustment。 And I dont want to project yet because I have to perform another grouping....help!
而且我不想投射,因为我必须执行另一个分组....帮助! :-)
:-)
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));
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, groups
is an IEnumerable<IGrouping<TPeriod, TAdjustment>>
where TPeriod
is the type of Period
(you didn't tell us) and TAdjustment
is the type of Adjustment
. 所以在上面的,
groups
是一个IEnumerable<IGrouping<TPeriod, TAdjustment>>
其中TPeriod
是类型Period
(你没有告诉我们)和TAdjustment
是类型Adjustment
。 Then, group
is an object that implements IEnumerable<TAdjustment>
(but it also has a Key
property so that you can get the key. Finally, item
is a TAdjustment
, and for each group
, all the item
that come from iterating over that group
have the same key. 然后,
group
是一个实现IEnumerable<TAdjustment>
的对象(但它也有一个Key
属性,这样你就可以获得密钥。最后, item
是一个TAdjustment
,对于每个group
,所有来自迭代该group
的item
都是拥有相同的钥匙。
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
}
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);
}
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.