共用方式為


分頁 FetchXML 大型結果集

 

發行︰ 2017年1月

適用於: Dynamics 365 (online)、Dynamics 365 (on-premises)、Dynamics CRM 2016、Dynamics CRM Online

使用分頁 Cookie,您可以分頁 FetchXML 查詢的結果。 分頁 Cookie 是效能功能,對非常大的資料集,可以讓應用程式的分頁更快。 當您查詢一組記錄,查詢結果會包含分頁 Cookie 的值。 如要改善效能,您可以在擷取下一組紀錄時傳送該值。

FetchXML 和 QueryExpression 針對其分頁 cookie 使用不同的格式。 如果您使用 FetchXmlToQueryExpressionRequest 訊息或 QueryExpressionToFetchXmlRequest 訊息將 A 查詢格式轉換為 B 格式,將會忽略分頁 Cookie 值。 此外,如果您要求不連續的頁面,會忽略分頁 Cookie 值。

當您使用分頁 Cookie 與 FetchXML,請務必使用正確的編碼。 下列範例顯示使用分頁 Cookie 與 FetchXML 時的正確編碼:

strQueryXML = @"
<fetch mapping='logical' paging-cookie='&lt;cookie page=&quot;1&quot;&gt;&lt;accountid last=&quot;{E062B974-7F8D-DC11-9048-0003FF27AC3B}&quot; first=&quot;{60B934EF-798D-DC11-9048-0003FF27AC3B}&quot;/&gt;&lt;/cookie&gt;' page='2' count='2'>
 <entity name='account'>
  <all-attributes/>
 </entity>
</fetch>";

下列範例顯示如何使用分頁 cookie 與 FetchXML 查詢。 如需完整的範例程式碼,請參閱範例:使用 FetchXML 搭配分頁 Cookie


// Define the fetch attributes.
// Set the number of records per page to retrieve.
int fetchCount = 3;
// Initialize the page number.
int pageNumber = 1;
// Initialize the number of records.
int recordCount = 0;
// Specify the current paging cookie. For retrieving the first page, 
// pagingCookie should be null.
string pagingCookie = null;

// Create the FetchXml string for retrieving all child accounts to a parent account.
// This fetch query is using 1 placeholder to specify the parent account id 
// for filtering out required accounts. Filter query is optional.
// Fetch query also includes optional order criteria that, in this case, is used 
// to order the results in ascending order on the name data column.
string fetchXml = string.Format(@"<fetch version='1.0' 
                                mapping='logical' 
                                output-format='xml-platform'>
                                <entity name='account'>
                                    <attribute name='name' />
                                    <attribute name='emailaddress1' />
                                    <order attribute='name' descending='false'/>
                                    <filter type='and'>
                            <condition attribute='parentaccountid' 
                                            operator='eq' value='{0}' uiname='' uitype='' />
                                    </filter>
                                </entity>
                            </fetch>",
                                _parentAccountId);

Console.WriteLine("Retrieving data in pages\n"); 
Console.WriteLine("#\tAccount Name\t\t\tEmail Address");

while (true)
{
    // Build fetchXml string with the placeholders.
    string xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);

    // Excute the fetch query and get the xml result.
    RetrieveMultipleRequest fetchRequest1 = new RetrieveMultipleRequest
    {
        Query = new FetchExpression(xml)
    };

    EntityCollection returnCollection = ((RetrieveMultipleResponse)_service.Execute(fetchRequest1)).EntityCollection;

    foreach (var c in returnCollection.Entities)
    {
        System.Console.WriteLine("{0}.\t{1}\t\t{2}", ++recordCount, c.Attributes["name"], c.Attributes["emailaddress1"] );
    }                        

    // Check for morerecords, if it returns 1.
    if (returnCollection.MoreRecords)
    {
        Console.WriteLine("\n****************\nPage number {0}\n****************", pageNumber);
        Console.WriteLine("#\tAccount Name\t\t\tEmail Address");

        // Increment the page number to retrieve the next page.
        pageNumber++;

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

另請參閱

範例:使用 FetchXML 搭配分頁 Cookie
使用 FetchXML 建立查詢
FetchXML 中的會計年度日期和 "older than" 日期/時間查詢運算子
使用 FetchXML 建構查詢
將大量結果集用 QueryExpression 分頁

Microsoft Dynamics 365

© 2017 Microsoft. 著作權所有,並保留一切權利。 著作權