Question

I have applied IGrouping<> over a list - here's what it looks like:

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 Tiers. Its simple to get the key data using the Key element, but how do I get the IEnumerable<PingtreeNode> that forms the value part?

Thanks in advance

Was it helpful?

Solution

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

OTHER TIPS

in foreach you can get values like this

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

The group itself implements IEnumerable<T> and can be iterated over, 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();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top