简体   繁体   English

如何从IGrouping中获取价值?

[英]How to get values out of IGrouping?

I have applied IGrouping<> over a list - here's what it looks like: 我在列表上应用了IGrouping <>-看起来像这样:

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? 使用Key元素获取密钥数据很简单,但是如何获取构成值部分的IEnumerable<PingtreeNode>

Thanks in advance 提前致谢

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

in foreach you can get values like this 在foreach中,您可以获得这样的值

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. 该组本身实现IEnumerable<T> ,可以被迭代,也可以与linq方法一起使用。

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM