C# 如果记录的记录数超过50000条,则获取合计记录数
我们的CRM实体中有大量记录。我试图通过fetchxml中的聚合计数来获取记录总数。但它有50000条记录的限制。我认为有一种方法可以改变内部CRM中的设置。但我不想改变这一点 以前我们使用分页方法获取总计数(每次5000)。但这需要很多时间C# 如果记录的记录数超过50000条,则获取合计记录数,c#,dynamics-crm,crm,dynamics-crm-2015,C#,Dynamics Crm,Crm,Dynamics Crm 2015,我们的CRM实体中有大量记录。我试图通过fetchxml中的聚合计数来获取记录总数。但它有50000条记录的限制。我认为有一种方法可以改变内部CRM中的设置。但我不想改变这一点 以前我们使用分页方法获取总计数(每次5000)。但这需要很多时间 public static int GetTotalRowCount(string fetchXml) { try { using (OrganizationServiceContext svcContext = new Organizat
public static int GetTotalRowCount(string fetchXml)
{
try
{
using (OrganizationServiceContext svcContext = new OrganizationServiceContext(ServerConnection.CrmService))
{
int totalCount = 0;
int fetchCount = 5000;
int pageNumber = 1;
string pagingCookie = null;
string xml = string.Empty;
RetrieveMultipleRequest fetchRequest1 = null;
EntityCollection entityCollection = null;
xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;
while (entityCollection.MoreRecords)
{
//moving to next page
pageNumber++;
xml = CreateXml(fetchXml, pagingCookie, pageNumber, fetchCount);
fetchRequest1 = new RetrieveMultipleRequest
{
Query = new FetchExpression(xml)
};
entityCollection = ((RetrieveMultipleResponse)svcContext.Execute(fetchRequest1)).EntityCollection;
totalCount = totalCount + entityCollection.Entities.Count;
}
return totalCount;
}
}
catch (Exception ex)
{
}
}
但这需要很多时间。因此,我将其更改为聚合计数方法-
像这样更改了Fetchxml-
<fetch mapping='logical' output-format='xml-platform' no-lock='true' distinct='false' aggregate='true'>
<entity name='abc_data'>
<attribute name='abc_id' aggregate='count' alias='count'/>.....
现在,如果记录超过50000条,它会给出一个例外
那么,有没有办法借助聚合计数一次获取50000条记录,并通过循环获取总计数?FetchXML聚合的限制是我们都面临的挑战。我想一劳永逸地解决这个问题,所以我构建了一个名为AggX的在线工具,在任意数量的行上运行聚合。它目前是免费使用的 您可以在上查看,请注意,它仅适用于Dynamics 365 Online。另外,请注意,如果您有大量的行需要超过5分钟才能运行,则应监视AggX,以避免它在几分钟空闲时间后自动将您注销
如果您的系统是基于prem的,或者您想编写自己的代码来进行聚合,那么您可以设计一种算法,将数据分割成少于50000行的块。然后可以对每个块运行FetchXML聚合,并对结果求和。这就是AggX引擎的工作原理。请参阅和了解如何为您的内部部署系统更改此功能。@omkar,一年前,我为D365使用了“Dynabacus解决方案”,其中它返回选定实体的记录总数(无任何限制),我试图为您找到该解决方案,但没有成功,出版商可能已经从他们的网站上删除了该解决方案。是的,我尝试过这样做。。将我的fetchxml更改为-。。这样我一次就能拿到50000块。。但它仍然返回超过50000FetchXML,不管我们设置的页面大小如何,它都被硬编码为停止在5000。您必须基于数据本身进行分块,例如“名称以‘A’开头”,然后“名称以‘B’开头”,等等。或者“一月创建”,然后在二月创建”等等。
int Count = 0;
FetchExpression fetch = new FetchExpression(fetchXml);
EntityCollection result = ServerConnection.CrmService.RetrieveMultiple(fetch);
if (result.Entities.Count > 0)
{
Entity entity = result.Entities[0];
AliasedValue value = (AliasedValue)entity["count"];
Count = (int)value.Value;
}
return Count ;