Page large result sets with LINQ

 

Applies To: Dynamics CRM 2015

In Microsoft Dynamics CRM 2015 and Microsoft Dynamics CRM Online 2015 Update you can page the results of a large .NET Language-Integrated Query (LINQ) query by using the Take and Skip operators. The Take operator retrieves a specified number of results and the Skip operator skips over a specified number of results.

LINQ paging example

The following example shows how to page the results of a LINQ query by using the Take and Skip operators:


int pageSize = 5;

var accountsByPage = (from a in svcContext.AccountSet
                      select new Account
                      {
                       Name = a.Name,
                      });
System.Console.WriteLine("Skip 10 accounts, then Take 5 accounts");
System.Console.WriteLine("======================================");
foreach (var a in accountsByPage.Skip(2 * pageSize).Take(pageSize))
{
 System.Console.WriteLine(a.Name);
}

' Retrieve records with Skip/Take record paging. Setting a page size
' can help you manage your Skip and Take calls, since Skip must be
' passed a multiple of Take's parameter value.
Dim pageSize As Integer = 5

Dim accountsByPage = ( _
    From a In svcContext.CreateQuery(Of Account)() _
    Select New Account With {.Name = a.Name})
Console.WriteLine("Skip 10 accounts, then Take 5 accounts")
Console.WriteLine("======================================")
For Each a In accountsByPage.Skip(2 * pageSize).Take(pageSize)
    Console.WriteLine(a.Name)
Next a
Console.WriteLine()
Console.WriteLine("<End of Listing>")
Console.WriteLine()

See Also

Build queries with LINQ (.NET language-integrated query)
LINQ query examples

© 2016 Microsoft. All rights reserved. Copyright