views:

39

answers:

3

I have code below that builds a collection and returns it sorted by a property

var appChanges = GetHistory().ToList();
return appChanges.OrderByDescending(r => r.Change.When);

i want this to only return a max of 50 items (or total if collection size is less than 50)

how can i do this in LINQ ?

+2  A: 
return appChanges.OrderByDescending(r => r.Change.When).Take(n);
desco
+4  A: 

You're looking for Take. See http://msdn.microsoft.com/en-us/library/bb503062.aspx

appChanges.OrderByDescending(r => r.Change.When).Take(50);
Rob
+4  A: 

Use the .Take(...) function.

You can also use .Skip(..) in conjunction with it for paging queries.

Also, you'll want to avoid using .ToList() so early if you can avoid it because it will evaluate the query and return a result set.

NickLarsen