开发者

select n largest using LINQ

This is likely a novice question about LINQ, but assuming I hav开发者_如何学Goe 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?


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.


yeah that's exactly how to do it.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜