I have a LINQ statement which pulls the top N record IDs from a collection and then another query which pulls all records which have those IDs. It feels very clunky and inefficient and i was wondering if there might be a more succinct, LINQy way to get the same results
var records = cache.Select(rec => rec.Id).Distinct().Take(n);
var results = cache.Where(rec => records.Contains(rec.Id));
FYI – there will be multiple records with the same ID, which is why there is the Distinct() and why i can’t use a simple Take() in the first place.
Thanks!
How about something like this?