102

I'm using Linq/EF4.1 to pull some results from a database and would like to limit the results to the (X) most recent results. Where X is a number set by the user.

Is there a way to do this?

I'm currently passing them back as a List if this will help with limiting the result set. While I can limit this by looping until I hit X I'd just assume not pass the extra data around.

Just in case it is relevant... C# MVC3 project running from a SQL Server database.

1
  • 4
    are you asking for linq "take(100)"?
    – daryal
    Commented Jun 4, 2012 at 15:01

3 Answers 3

184

Use the Take function

int numberOfrecords=10; // read from user
listOfItems.OrderByDescending(x => x.CreatedDate).Take(numberOfrecords)

Assuming listOfItems is List of your entity objects and CreatedDate is a field which has the date created value (used here to do the Order by descending to get recent items).

Take() Function returns a specified number of contiguous elements from the start of a sequence.

http://msdn.microsoft.com/en-us/library/bb503062.aspx

8
  • 1
    "most recent results" - that's not addressed by your answer.
    – Mr. TA
    Commented Jun 4, 2012 at 15:02
  • @Mr.TA: Ah ! I missed that part. Updated now. Thanks for pointing out.
    – Shyju
    Commented Jun 4, 2012 at 15:04
  • 2
    While I didn't really need help with the most recent portion (I worded the question poorly) the take is what I needed! Thanks!
    – Jared
    Commented Jun 4, 2012 at 15:06
  • 1
    As soon as the dumb wait to select answer timer runs out I'll select your answer... thanks again
    – Jared
    Commented Jun 4, 2012 at 15:07
  • @Shyju nope doesn't let me to, saying that the answer needs to be edited. Weird.
    – Mr. TA
    Commented Jun 4, 2012 at 15:10
41
results = results.OrderByDescending(x=>x.Date).Take(10);

The OrderByDescending(...) will sort items by your date/time property (or w/e logic you want to use to get most recent) and Take(...) will limit to first x items (first being most recent, thanks to the ordering).

Edit: To return some rows not starting at the first row, use Skip():

results = results.OrderByDescending(x=>x.Date).Skip(50).Take(10);
2
  • 1
    Is it possible to take results starting with some index? For example, one hundred results - start with index 50, take 10 results, from 50 - 60?
    – FrenkyB
    Commented Nov 4, 2016 at 13:20
  • 2
    @FrenkyB please see the answer - I updated it to address your follow up.
    – Mr. TA
    Commented Nov 4, 2016 at 15:09
23

Use Take(), before converting to a List. This way EF can optimize the query it creates and only return the data you need.

3
  • 1
    Would you kindly elaborate more. Maybe with an example?
    – W.M.
    Commented Mar 31, 2017 at 8:34
  • 4
    @W.M. Using Take first will allow EF to do the paging right in the SQL query. If you do ToList first instead, all results will be pulled back from SQL, and paging will then be done in memory. Commented Apr 4, 2017 at 15:06
  • @RedFilter is the only person that addressed my concern. I've got a set of 31000 items... it seemed wasteful to grab the entire list, just so i can pull the top 10 off the list. However, I wouldn't mind seeing an example :)
    – Joshua K
    Commented Aug 28, 2018 at 14:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.