23

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.

PFranchise
  • 6,642
  • 11
  • 56
  • 73

3 Answers3

54

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)
Łukasz W.
  • 9,538
  • 5
  • 38
  • 63
5

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
  • 242,243
  • 40
  • 408
  • 536
4
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
  • 16,498
  • 6
  • 59
  • 85