Up LINQ query examples

Paging operators

Last modified on August 15, 2012 17:21

Examples of the LINQ Take and Skip operators are shown below.  In the examples below _em1 is an EntityManager.

C#
[Description("This sample uses WHERE to find all customers whose contact title is Sales Representative.")]
public void LinqToEntities96a() {

 // Not a variation of LinqToEntities96, but no numbers were available here.

  var customersQuery = _em1.Customers.OrderBy(c => c.CompanyName);
  customersQuery.QueryStrategy = QueryStrategy.DataSourceOnly;
  ICollection<Customer> customers = customersQuery.Skip(5).Take(5).ToList();
  Assert.IsTrue(customers.Count() == 5);
  customersQuery.QueryStrategy = QueryStrategy.CacheOnly;
  Assert.IsTrue(customersQuery.Count() == 5);
}

[Description("Skip the most recent 2 orders from customers in London")]
public void LinqToEntities96() {
  var query = _em1.Orders
      .Where(o => o.Customer.Address.City == "London")
      .OrderBy(o => o.OrderDate)
      .Skip(2).Select(o => o);

  Assert.IsTrue(query.First().OrderID == 10359);
}

[Description("Take the 2 most recent Orders ")]
public void LinqToEntities97() {
  var query = _em1.Orders
      .OrderBy(o => o.OrderDate)
      .Take(2).Select(o => o);

  Assert.IsTrue(query.Count() == 2);
  Assert.IsTrue(query.First().OrderID == 10248);
}

[Description("Take the 10th to the 20th Orders, ordered by date ")]
public void LinqToEntities98() {
  var query = _em1.Orders
      .OrderBy(o => o.OrderDate)
      .Skip(10).Take(10).Select(o => o);

  query.QueryStrategy = QueryStrategy.DataSourceOnly; // because of skip operator

  var r = query.ToList();
  Assert.IsTrue(r.Count() == 10);
  Assert.IsTrue(r.First().OrderID == 10258);
}

[Description("Use a page number variable to get the xth page")]
public void LinqToEntities99() {
 int pageSize = 10;
 int pageNumber = 4;

  var query = _em1.Orders
      .OrderBy(o => o.OrderDate)
      .Skip(pageSize * pageNumber).Take(pageSize).Select(o => o);
  query.QueryStrategy = QueryStrategy.DataSourceOnly;
  var r = query.ToList();
  Assert.IsTrue(r.Count() == pageSize);
  Assert.IsTrue(r.First().OrderID == 10288);
}
VB
<Description("This sample uses WHERE to find all customers whose contact title is Sales Representative.")>
Public Sub LinqToEntities96a()

 ' Not a variation of LinqToEntities96, but no numbers were available here.

 Dim customersQuery = _em1.Customers.OrderBy(Function(c) c.CompanyName)
  customersQuery.QueryStrategy = QueryStrategy.DataSourceOnly
 Dim customers As ICollection(Of Customer) = customersQuery.Skip(5).Take(5).ToList()
  Assert.IsTrue(customers.Count() = 5)
  customersQuery.QueryStrategy = QueryStrategy.CacheOnly
  Assert.IsTrue(customersQuery.Count() = 5)
End Sub

<Description("Skip the most recent 2 orders from customers in London")>
Public Sub LinqToEntities96()
 Dim query = _em1.Orders.Where(Function(o) o.Customer.Address.City = _
   "London").OrderBy(Function(o) o.OrderDate).Skip(2).Select(Function(o) o)

  Assert.IsTrue(query.First().OrderID = 10359)
End Sub

<Description("Take the 2 most recent Orders ")>
Public Sub LinqToEntities97()
 Dim query = _em1.Orders.OrderBy(Function(o) o.OrderDate).Take(2).Select(Function(o) o)

  Assert.IsTrue(query.Count() = 2)
  Assert.IsTrue(query.First().OrderID = 10248)
End Sub

<Description("Take the 10th to the 20th Orders, ordered by date ")>
Public Sub LinqToEntities98()
 Dim query = _em1.Orders.OrderBy(Function(o) o.OrderDate).Skip(10).Take(10).Select(Function(o) o)

  query.QueryStrategy = QueryStrategy.DataSourceOnly ' because of skip operator

 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = 10)
  Assert.IsTrue(r.First().OrderID = 10258)
End Sub

<Description("Use a page number variable to get the xth page")>
Public Sub LinqToEntities99()
 Dim pageSize As Integer = 10
 Dim pageNumber As Integer = 4

 Dim query = _em1.Orders.OrderBy(Function(o) o.OrderDate).Skip(pageSize * _
    pageNumber).Take(pageSize).Select(Function(o) o)
  query.QueryStrategy = QueryStrategy.DataSourceOnly
 Dim r = query.ToList()
  Assert.IsTrue(r.Count() = pageSize)
  Assert.IsTrue(r.First().OrderID = 10288)
End Sub
Created by DevForce on February 18, 2011 09:35

This wiki is licensed under a Creative Commons 2.0 license. XWiki Enterprise 3.2 - Documentation. Copyright © 2020 IdeaBlade