How to get values ​​from IGrouping?

I applied IGrouping <> to the list - this is how it looks:

IEnumerable<IGrouping<TierRequest,PingtreeNode>> Tiers
{
    get { return ActiveNodes.GroupBy(x => new TierRequest(x.TierID, x.TierTimeout, x.TierMaxRequests)); }
}

Later in my code, I iterate over the levels. Getting key data is simple using the Key element, but how do I get IEnumerable<PingtreeNode>which forms part of the value?

thank you in advance

+4
source share
3 answers

Tiers.Select(group => group.Select(element => ...));

+4
source

in foreach you can get values ​​like this

foreach(var group in tiers)
{
    TierRequest key = group.Key;
    PingtreeNode[] values = group.ToArray();
}
+4
source

The group itself implements IEnumerable<T>and can be repeated or used with linq methods.

var firstGroup = Tiers.First();

foreach(var item in firstGroup)
{
  item.DoSomething();
}

// or using linq:
firstGroup.Select(item => item.ToString());

// or if you want to iterate over all items at once (kind of unwinds
// the grouping):
var itemNames = Tiers.SelectMany(g => g.ToString()).ToList();
+3
source

All Articles