简体   繁体   中英

Linq query for only the first N rows for each unique ID

Say I have an IQueryable that will return a datatype with an ID property (column).

I want to further filter my query ( I don't want to evaluate the query ) as follows:

For each unique ID from the main query, I want to Take(n) , where n is some arbitrary number.

That is, I want to only keep the first n rows for each unique ID.

I can get the distinct ID 's...

var ids = query.Select(q => q.ID).Distinct();

and I can Take(n) with the rest of them, but I'm stumped on connecting the two:

query = query.<FOR EACH DISTINCT ID>.Take(n);

The accepted answer works, but is slow for a large table. I wrote this question as a follow-up.

You can do it like this:

query = query.GroupBy(q => q.ID).SelectMany(g => g.Take(n));

The GroupBy brings together the records with identical ID s, letting you process them as a group; SelectMany takes each group, limits the number of its members to n , and puts the results back into one flat list.

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