CRM 2015 Linq Count()查询-这是枚举查询吗? [英] CRM 2015 Linq Count() query - is this enumerating the query?

查看:158
本文介绍了CRM 2015 Linq Count()查询-这是枚举查询吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在努力针对Dynamics CRM 2015 SDK OrganizationServiceContext 编写Linq查询。我发现CRM Linq提供程序通常不支持我要使用的Linq方法。

I've been struggling with writing Linq queries against the Dynamics CRM 2015 SDK OrganizationServiceContext. I'm finding that often the Linq methods I want to use are not supported by the CRM Linq Provider.

最新的是 Count()。我有一个 IQuerable< Lead> 对象,并希望计算枚举时将返回的总记录。但是在 IQueryable 上调用 Count()会产生错误:

The latest is Count(). I have an IQuerable<Lead> object and want to count the total records that would be returned when enumerated. But calling Count() on an IQueryable gives the error:


不支持'Count'方法

The method 'Count' is not supported

在挖掘中发现此建议将Linq查询定义为 IEnumerable 。这似乎可行-在 IEnumerable< Lead> 对象上调用 Count()返回总记录。

Digging around I found this advice which is to define the Linq query as an IEnumerable. This appears to work - calling Count() on the IEnumerable<Lead> object returns the total records.

我想知道的是枚举操作在哪里进行。是Dynamics方面,还是将所有潜在客户拉入我的Web服务器内存并在那里计数?首先执行计数的全部原因是为了防止将太大的数据集拉入内存...

What I'd like to know is where the enumeration operation takes place. Is it Dynamics side, or is it pulling all leads into my web server memory and counting there? The whole reason I'm performing the count in the first place is to guard against pulling too large a dataset into memory...

推荐答案

LINQ for CRM查询被转换为 QueryExpression 查询,因此限于其功能。 QueryExpression查询不支持聚合(如计数,总和,平均值),因此确实您的查询会将所有选定的记录拉入内存。

LINQ for CRM queries are translated to QueryExpression queries and therefore are limited to its capabilities. QueryExpression queries do not support aggregates (like count, sum, average), so indeed your query will pull all selected records into memory.

获取正确计数的首选方法正在使用FetchXml查询。

The preferred method to get a proper count is using a FetchXml query.

使用 QueryExpression 进行计数也可以通过以下方式实现。 (我不确定这是如何转换成SQL的;它的性能可能会稍差。)

A count with QueryExpression can also be achieved in the following way. (I am not sure how this is translated into SQL; it may be slightly less performant.)

这里有个例子:

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;

可以计数的记录数是有限的。我相信目前是50,000。在OnPremise部署中,您可以配置此限制。

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

这篇关于CRM 2015 Linq Count()查询-这是枚举查询吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆