select n largest using LINQ
        Posted  
        
            by Mathias
        on Stack Overflow
        
        See other posts from Stack Overflow
        
            or by Mathias
        
        
        
        Published on 2010-05-10T23:52:47Z
        Indexed on 
            2010/05/10
            23:54 UTC
        
        
        Read the original article
        Hit count: 348
        
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?
© Stack Overflow or respective owner