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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With