tags:

views:

161

answers:

3

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?

+5  A: 

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.

Aaronaught
The DataContext is coming from EF. And thanks for both useful comments on indexing on dates, and on using ToList(). In this specific case, I do need a list, even though the reason is not mentioned in the problem description!
Mathias
+2  A: 

I think this is fine. You could also do this in 1 sentence:

 var recentItems = (from item in dataContext.Items
              where item.Date<=date 
              orderby item.Date descending 
              select item).Take(5).ToList();

but your way is equally good.

eKek0
+2  A: 

yeah that's exactly how to do it.

Ben Robinson