简体   繁体   English

如何将列表分组 <int> 并返回一个列表 <int>

[英]how do i group a List<int> and return a List<int>

I have a list of ints. 我有一个整数列表。 I want to group the list and create a new list that contains only the grouped by number that meets a certain condition. 我想对列表进行分组并创建一个新列表,该列表仅包含符合特定条件的按数字分组的列表。 this is what i have so far. 这是我到目前为止所拥有的。 the declarating for membersList is List 对membersList的声明是List

int rows = 5;

List<int> memberKeys = memberKeysList
  .GroupBy(x => x)
  .Where(x => x.Count() == rows)
  .ToList();

Its complaining about converting from groupedby list to a list. 它抱怨从groupedby列表转换为列表。

You need to Select the Key to get the number like: 您需要SelectKey来获取数字,例如:

List<int> memberKeys = memberKeysList.GroupBy(x => x)
                           .Where(x => x.Count() == rows)
                           .Select(grp => grp.Key)
                           .ToList();

If you are not going to explicitly select they Key (or the number) , then the result of GroupBy clause would be IEnumerable<IGrouping<TKey, TElement>> 如果不打算明确选择它们的Key (或数字) ,则GroupBy子句的结果将为IEnumerable<IGrouping<TKey, TElement>>

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

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