views:

59

answers:

3

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.

+4  A: 

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)
ŁukaszW.pl
Thanks Lukasz! I appreciate it. I'll mark yours as answer as soon as it lets me.
PFranchise
Thanks for the edit, that will come in handy for another query I will have to make.
PFranchise
That's going to select everything from the table before restricting the results in memory.
David Neale
David.. no, its not.. Its querable object.. its equivalent to query... It will execute when he will try to get some objects or use ToList method etc.
ŁukaszW.pl
@David, what in that original query forces execution?
Marc
@LukaszW.pl, sorry I didn't realise that.
David Neale
+2  A: 

What you're looking for is Take:

var productQuery = (from b in solutionContext.Version
                   where b.Product.ID != 1 
                       && b.VersionNumber == b.Product.ActiveNumber
                   orderby b.Product.LastNumber
                   select b).Take(25);
Justin Niessner
Thanks Justin! I appreciate it.
PFranchise
+1  A: 
var productQuery = (from b in solutionContext.Version
                           where b.Product.ID != 1 && b.VersionNumber == b.Product.ActiveNumber
                           orderby b.Product.LastNumber
                           select b).Take(25);
David Neale