user327999
user327999

Reputation: 453

C# LINQ SPROC Getting Results FROM page number

I am using LINQ TO SQL in a ASP.NET MVC application and my code looks something like this

ISingleResult<proc_get_dataResult> sproc_result = db.proc_get_data();
return sproc_result .AsEnumerable().Cast<proc_get_dataResult>().ToList();

My code above returns the entire result set as a list. However, the query in question can return a ton of data so I want to limit the number of objects that are being created. Therefore, I now pass a page number into this method and if for example I pass in page 2, I would only like to return entries 26-50 if the per page constant is set to 25.

Is there a way to do this?

thanks in advance

Upvotes: 0

Views: 106

Answers (2)

Rick Petersen
Rick Petersen

Reputation: 744

Well, it will be questionably efficient but it appears to be part of your requirement to query and bring back all the results. From there you can use the Skip() and Take() extension methods in LINQ to get what you want:

 return sproc_result.AsEnumerable().Cast<proc_get_dataResult>().ToList().OrderBy(r=>r.WhateverField).Skip(25).Take(25);

Edit: I forgot you have to OrderBy before you can Skip or Take. Just substitute whatever the ordered field is.

Upvotes: 1

3Dave
3Dave

Reputation: 29071

See this thread.

This isn't an exact duplicate since you're asking in the context of LINQ. You'll need to modify your sproc to use one of the paging methods in that thread. The CTE / Row_Number() over... method is a pretty efficient way of assigning indices to your result set that can then be used to sort/page your results.

Upvotes: 0

Related Questions