5

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

Timo Salomäki
  • 7,099
  • 3
  • 25
  • 40
dotnetnoob
  • 10,783
  • 20
  • 57
  • 103

3 Answers3

9

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

Vladimir
  • 7,345
  • 4
  • 34
  • 39
4

in foreach you can get values like this

foreach(var group in tiers)
{
    TierRequest key = group.Key;
    PingtreeNode[] values = group.ToArray();
}
maxlego
  • 4,864
  • 3
  • 31
  • 38
4

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();
Anders Abel
  • 67,989
  • 17
  • 150
  • 217