CRM 2015 Linq Count () query is query enumeration?

I was struggling with writing Linq queries against the Dynamics CRM 2015 SDK OrganizationServiceContext

. I have found that often the Linq methods I want to use are not supported by the CRM Linq provider.

The last one is << 21>. I have an object IQuerable<Lead>

and want to count the total records that will be returned when enumerated. But the challenge Count()

to IQueryable

give an error:

"Count" method is not supported

Digging around I found this tip , which is to define a Linq query as IEnumerable

. This seems to work - calling the Count()

object IEnumerable<Lead>

returns the totals.

I would like to know where the enumeration operation is happening. Is it the dynamic side, or is it pulling all inputs into my web server memory and counting there? The whole reason I am doing the counting in the first place is to avoid pulling too large a set of data into memory ...

+3


source to share


1 answer


LINQ for CRM queries translate into queries QueryExpression

and are therefore limited by its capabilities. QueryExpression queries do not support aggregates (e.g. count, sum, average), so your query will retrieve all selected records into memory.

The preferred method for getting the correct invoice is to use a FetchXml query.

Counter with QueryExpression

can also be reached as follows. (I'm not sure how this translates to SQL, it might be slightly less efficient).



Here's an example:

var query = new QueryExpression("account")
{
    PageInfo = new PagingInfo
    {
        Count = 1,
        PageNumber = 1,
        ReturnTotalRecordCount = true
    }
};

var result = service.RetrieveMultiple(query);

if (result.TotalRecordCountLimitExceeded)
    throw new InvalidOperationException("Cannot get record count.");

return result.TotalRecordCount;

      

As you can see, there is a limit to the number of records that can be counted. I believe this is currently 50,000. In OnPremise deployments, you can adjust this limit.

+5


source







All Articles