Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

select n largest using LINQ

Tags:

c#

.net

linq

This is likely a novice question about LINQ, but assuming I have a set of Items with a DateTime property, one date having at most one item, how would I go about selecting the N most recent items from a date of reference, that is, the N items which have a date smaller that the requested date, and the largest date?
My naive thought would be to first select items with a date smaller than the reference date, sort by date, and select the N first items from that subset.

 var recentItems = from item in dataContext.Items
              where item.Date<=date 
              orderby item.Date descending 
              select item;

 var mostRecentItems = recentItems.Take(5).ToList();

Is this the "right" way to do it, or are there obviously better ways to achieve my goal?

like image 282
Mathias Avatar asked Dec 28 '22 17:12

Mathias


1 Answers

Yes, that is the correct method. Judging by the word dataContext in there, I'm assuming that this is Linq to SQL code; it will get turned into an efficient TOP N query.

(Well, "efficient" as long as the data is indexed by date.)

One thing I might change is to remove the ToList(). Unless you actually need it to be a list, it is typically better to leave it alone as an IEnumerable<T>, especially if you just need to iterate over it as opposed to obtaining elements by index.

Edit: I should qualify what I mean by better in reference to ToList. When you call ToList, you end up with an in-memory structure that contains all of the elements in the set. This uses up memory that you don't always need to be using. Creating the list also requires a full iteration of all records, so if you later iterate the list itself, you've gone through every element twice.

For only 5 elements, the difference probably will not be noticeable; however, for 5000 elements it may matter quite a lot. Therefore, you should get in the habit of not using ToList() unless you are certain that you need it. Most of the time, you don't.

like image 111
Aaronaught Avatar answered Dec 31 '22 07:12

Aaronaught