简体   繁体   English

如何从 IGrouping 中获取值

[英]How to get values from IGrouping

I have a question about IGrouping and the Select() method.我对IGroupingSelect()方法有疑问。

Let's say I've got an IEnumerable<IGrouping<int, smth>> in this way:假设我有一个IEnumerable<IGrouping<int, smth>>是这样的:

var groups = list.GroupBy(x => x.ID);

where list is a List<smth> .其中list是一个List<smth>

And now I need to pass values of each IGrouping to another list in some way:现在我需要以某种方式将每个IGrouping的值传递给另一个列表:

foreach (var v in structure)
{
    v.ListOfSmth = groups.Select(...); // <- ???
}

Can anybody suggest how to get the values ( List<smth> ) from an IGrouping<int, smth> in such a context?有人可以建议如何在这种情况下从IGrouping<int, smth>获取值( List<smth> )吗?

Since IGrouping<TKey, TElement> implements IEnumerable<TElement> , you can use SelectMany to put all the IEnumerables back into one IEnumerable all together:由于IGrouping<TKey, TElement>实现了IEnumerable<TElement> ,您可以使用SelectMany将所有IEnumerables放回一个IEnumerable

List<smth> list = new List<smth>();
IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.id);
IEnumerable<smth> smths = groups.SelectMany(group => group);
List<smth> newList = smths.ToList();

Here's an example that builds/runs: https://dotnetfiddle.net/DyuaaP这是构建/运行的示例: https : //dotnetfiddle.net/DyuaaP

Video commentary of this solution: https://youtu.be/6BsU1n1KTdo本方案视频解说: https : //youtu.be/6BsU1n1KTdo

foreach (var v in structure) 
{     
    var group = groups.Single(g => g.Key == v. ??? );
    v.ListOfSmth = group.ToList();
}

First you need to select the desired group.首先,您需要选择所需的组。 Then you can use the ToList method of on the group.然后就可以使用组上的ToList方法了。 The IGrouping is a IEnumerable of the values. IGrouping是值的IEnumerable

More clarified version of above answers:上述答案的更明确版本:

IEnumerable<IGrouping<int, ClassA>> groups = list.GroupBy(x => x.PropertyIntOfClassA);

foreach (var groupingByClassA in groups)
{
    int propertyIntOfClassA = groupingByClassA.Key;

    //iterating through values
    foreach (var classA in groupingByClassA)
    {
        int key = classA.PropertyIntOfClassA;
    }
}

From definition of IGrouping :从 IGrouping 的定义:

IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable

you can just iterate through elements like this:你可以遍历这样的元素:

IEnumerable<IGrouping<int, smth>> groups = list.GroupBy(x => x.ID)
foreach(IEnumerable<smth> element in groups)
{
//do something
}
var groups = list.GroupBy(x => x.ID);

Can anybody suggest how to get the values (List) from an IGrouping<int, smth> in such a context?任何人都可以建议如何在这种情况下从 IGrouping<int, smth> 获取值(列表)?

"IGrouping<int, smth> group" is actually an IEnumerable with a key, so you either: “IGrouping<int, smth> group”实际上是一个带键的 IEnumerable,所以你要么:

  • iterate on the group or迭代组或
  • use group.ToList() to convert it to a List使用 group.ToList() 将其转换为 List
foreach (IGrouping<int, smth> group in groups)
{
   var thisIsYourGroupKey = group.Key; 
   List<smth> list = group.ToList();     // or use directly group.foreach
}

If you have an IGrouping<GroupItem, ListItem> , and you want to access the items of type ListItem of this group without utilizing a foreach loop, it's very simple.如果您有一个IGrouping<GroupItem, ListItem> ,并且您想要在不使用foreach循环的情况下访问该组ListItem类型的项目,这非常简单。 The object of type IGrouping<GroupItem, ListItem> is of type IEnumerable<ListItem> as well, as it is defined as: IGrouping<GroupItem, ListItem>类型的 object 也是IEnumerable<ListItem>类型,因为它定义为:

public interface IGrouping<out TKey, out TElement> : IEnumerable<TElement>, IEnumerable

So you can simply say:所以你可以简单地说:

foreach (IGrouping<GroupItem, ListItem> group in list.GroupBy(x => x.ID))
{
    IEnumerable<ListItem> itemsInThisGroup = group;
    // ...
}

If for some reason, it has to be a List<T> instead of an IEnumerable<T> , you can of course still call itemsInThisGroup.ToList() .如果出于某种原因,它必须List<T>而不是IEnumerable<T> ,您当然仍然可以调用itemsInThisGroup.ToList() But usually it's better not to if you needn't.但如果不需要,通常最好不要这样做。

Simply do this:只需这样做:

// this will "split" the list into groups
var groups = list.GroupBy(x => x.ID);

// groups is a "collection of lists"
foreach (var sublist in groups)
{
  // now the sublist is only a part of the original list
  // to get which is the value of ID, you can use sublist.Key
}

You don't need Select().GroupBy(expr) makes "a list of lists", kind of.您不需要Select().GroupBy(expr)制作“列表列表”,有点。

Assume that you have MyPayments class like假设你有 MyPayments 类

 public class Mypayment
{
    public int year { get; set; }
    public string month { get; set; }
    public string price { get; set; }
    public bool ispaid { get; set; }
}

and you have a list of MyPayments你有一个 MyPayments 列表

public List<Mypayment> mypayments { get; set; }

and you want group the list by year.并且您希望按年份对列表进行分组。 You can use linq like this:您可以像这样使用 linq:

List<List<Mypayment>> mypayments = (from IGrouping<int, Mypayment> item in yearGroup
                                                let mypayments1 = (from _payment in UserProjects.mypayments
                                                                   where _payment.year == item.Key
                                                                   select _payment).ToList()
                                                select mypayments1).ToList();

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

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