Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Limiting query size with entity framework

this is a simple question (I think), but I have not been able to find a solution. I know with other types of queries, you can add a limit clause that makes the query only return up to that many results. Is this possible with an entity query?

var productQuery = from b in solutionContext.Version
                               where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
                               orderby b.Product.LastNumber
                               select b;

I just want to make it so this query only returns 25 version objects. Thanks for any help.

like image 223
PFranchise Avatar asked Jul 27 '10 14:07

PFranchise


People also ask

Is entity framework good for large database?

Yes, (In some cases stored procedures are a better choice, they are not that evil as some make you believe), you should use stored procedures where necessary. Import them into your model and have function imports for them.


1 Answers

sure.. for example you can do it like this:

var productQuery = from b in solutionContext.Version
                           where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
                           orderby b.Product.LastNumber
                           select b;

var limitedProductQuery = productQuery.Take(25);

also you may need this for paging results:

var pagedProductQuery = productQuery.Skip(25 * page).Take(25)
like image 58
Łukasz W. Avatar answered Sep 23 '22 15:09

Łukasz W.