简体   繁体   中英

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

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.

You need to Select the Key to get the number like:

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>>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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