问题
I am using the CRM 2011 SDK to work with a remote CRM 2011 service. I need to retrieve the total number of contacts, which is something over 20000.
I've tried a simple LINQ query, eg. Aggregate c In service.ContactSet Into Count() (from these examples, also this suggestion and this suggestion) but each one took ages, because the resulting XML that gets downloaded is an entire recordset of data (even if it's only the GUID, that's still 20000 GUIDs and all their XML fluff).
I can't seem to find how to simply query CRM 2011 in such a way as it returns a single number - the total count of records in an entity. A "scalar query" so to speak. How is this done?
回答1:
Unfortunately, LINQ queries does not support aggregates and grouping.
The documentation says:
FetchXML Supports all the features of QueryExpression plus aggregates and grouping. Queries are built as XML statements.
So, the only option you have is using FetchXML as follow:
string fetchQuery = @"<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='entity name'>
<attribute name='attribute name' aggregate='count' alias='aliasName'/>
</entity>
</fetch>";
EntityCollection value = _serviceProxy.Execute(new FetchExpression(fetchQuery));
Returned result will be something as follow:
<resultset morerecords="0"'>
<result>
<aliasName>20</aliasName>
</result>
</resultset>
来源:https://stackoverflow.com/questions/11144390/crm-2011-sdk-obtaining-count-of-entities-without-retrieving-rows-of-data