使用查询表达式对大型结果集分页

 

发布日期: 2017年1月

适用于: Dynamics 365 (online),Dynamics 365 (on-premises),Dynamics CRM 2016,Dynamics CRM Online

在 Microsoft Dynamics 365(在线或本地) 中,可以使用分页 Cookie 功能在大型数据集的应用程序中进行更快速的分页。FetchXML 和 QueryExpression 查询中都提供了该功能。 如果在查询记录集时使用分页 Cookie 功能,则结果会包含分页 Cookie 值。 若要提高系统性能,可以在检索下一个记录集时传递该值。

QueryExpression 和 FetchXML 对其分页 Cookie 使用不同的格式。 如果您通过 QueryExpressionToFetchXmlRequest 消息或 FetchXmlToQueryExpressionRequest 消息转换查询格式,则会忽略分页 Cookie 值。 另外,如果您请求不连续的页,也会忽略分页 Cookie 值。

以下示例演示如何将分页 Cookie 用于查询表达式。 有关完整示例代码的信息,请参阅示例:将 QueryExpression 与分页 Cookie 结合使用


// Query using the paging cookie.
// Define the paging attributes.
// The number of records per page to retrieve.
int queryCount = 3;

// Initialize the page number.
int pageNumber = 1;

// Initialize the number of records.
int recordCount = 0;

// Define the condition expression for retrieving records.
ConditionExpression pagecondition = new ConditionExpression();
pagecondition.AttributeName = "parentaccountid";
pagecondition.Operator = ConditionOperator.Equal;
pagecondition.Values.Add(_parentAccountId);

// Define the order expression to retrieve the records.
OrderExpression order = new OrderExpression();
order.AttributeName = "name";
order.OrderType = OrderType.Ascending;

// Create the query expression and add condition.
QueryExpression pagequery = new QueryExpression();
pagequery.EntityName = "account";
pagequery.Criteria.AddCondition(pagecondition);
pagequery.Orders.Add(order);
pagequery.ColumnSet.AddColumns("name", "emailaddress1");                   

// Assign the pageinfo properties to the query expression.
pagequery.PageInfo = new PagingInfo();
pagequery.PageInfo.Count = queryCount;
pagequery.PageInfo.PageNumber = pageNumber;

// The current paging cookie. When retrieving the first page, 
// pagingCookie should be null.
pagequery.PageInfo.PagingCookie = null;
Console.WriteLine("Retrieving sample account records in pages...\n");
Console.WriteLine("#\tAccount Name\t\tEmail Address"); 

while (true)
{
    // Retrieve the page.
    EntityCollection results = _serviceProxy.RetrieveMultiple(pagequery);
    if (results.Entities != null)
    {
        // Retrieve all records from the result set.
        foreach (Account acct in results.Entities)
        {
            Console.WriteLine("{0}.\t{1}\t{2}", ++recordCount, acct.Name,
                               acct.EMailAddress1);
        }
    }

    // Check for more records, if it returns true.
    if (results.MoreRecords)
    {
        Console.WriteLine("\n****************\nPage number {0}\n****************", pagequery.PageInfo.PageNumber);
        Console.WriteLine("#\tAccount Name\t\tEmail Address");

        // Increment the page number to retrieve the next page.
        pagequery.PageInfo.PageNumber++;

        // Set the paging cookie to the paging cookie returned from current results.
        pagequery.PageInfo.PagingCookie = results.PagingCookie;
    }
    else
    {
        // If no more records are in the result nodes, exit the loop.
        break;
    }
}

另请参阅

使用 QueryExpression 构建查询
示例:将 QueryExpression 与分页 Cookie 结合使用
示例:使用一对多关系进行检索
使用 QueryExpression 类
使用 FetchXML 对大型结果集进行分页

Microsoft Dynamics 365

© 2017 Microsoft。 保留所有权利。 版权