简体   繁体   English

如何迭代IGrouping <T>接口?

[英]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 ,所有来自迭代该groupitem都是拥有相同的钥匙。

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.

相关问题 我如何排序列表 <IGrouping<string, XElement> &gt;以自定义顺序? - How do I sort a List<IGrouping<string, XElement>> in custom order? 如何 map IGrouping<t, k> 到 IGrouping<t, v> 使用 Linq?</t,></t,> - How to map IGrouping<T, K> to IGrouping<T, V> using Linq? 如何转换IOrderedEnumerable <T> 和IEnumerable <IGrouping<T> &gt;&IEnumerable <T> 到一种常见格式? - How can I convert IOrderedEnumerable<T> & IEnumerable<IGrouping<T>> & IEnumerable<T> to one common format? 如何将IGrouping &lt;&gt;&#39;对象&#39;添加到列表中? - How do you add an IGrouping<> 'object' to a list? 如何定义IList <T> 在界面中 - How do I define IList<T> in Interface C#如何使用Lambda表达式将iGrouping返回到列表 - C# How do i return iGrouping to a list using lambda expression 将每个T转换为IEnumerable <IGrouping<,> &gt;到IGrouping &lt;,&gt; - Cast each T in the IEnumerable<IGrouping<,>> to IGrouping<,> 在MVC5剃刀视图上分组-如何将Linq.IGrouping转换为IEnumerable-我是如此接近,我可以品尝到它 - Grouping on MVC5 Razor View - How do I convert Linq.IGrouping to IEnumerable - I am so close I can taste it 如何存储IGrouping列表 <int, object> 单个列表 <T> ? - How to store a List of IGrouping<int, object> to single List<T>? 如何访问IGrouping的成员? - How to access the member of an IGrouping?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM