How do I iterate an IGrouping Interface?

前端 未结 5 753
逝去的感伤
逝去的感伤 2020-12-24 00:34

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

5条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-24 01:07

    Each element of a sequence of IGrouping is an IEnumerable that you can iterate over to get the data that has a common 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> where TPeriod is the type of Period (you didn't tell us) and TAdjustment is the type of Adjustment. Then, group is an object that implements IEnumerable (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.

提交回复
热议问题