统计订购产品的数量
本文关键字:统计 | 更新日期: 2023-09-27 17:49:26
我试图获得与订单相关的订单产品的计数。下面是我的代码:
//count the number of lines in the actual order
QueryExpression query = new QueryExpression("salesorderdetail");
query.ColumnSet.AddColumns("salesorderid");
query.Criteria = new FilterExpression();
query.Criteria.AddCondition("salesorderid", ConditionOperator.Equal, order.Id);
EntityCollection response = service.RetrieveMultiple(query);
由于某些原因,当我使用响应。TotalRecordCount我有-1作为计数,即使有一些记录。我猜-1和一个问题有关,这个表达式有什么问题吗?
您可以/应该使用FetchXML来执行查询,因为您正在减少从CRM发送到本地机器的数据量。下面是一个示例fetch语句,它将执行您想要的操作
<fetch mapping="logical" aggregate='true'>
<entity name="salesorderdetail">
<attribute name="salesorderid" aggregate="countcolumn" alias='test'/>
<filter>
<condition attribute="salesorderid" operator="eq" value="e076c06a-6609-4aca-b0f4-a5075997378b" />
</filter>
</entity>
我为IOrganizationService编写了一个扩展方法。
这样调用它:
Console.WriteLine("# of Account records: {0}",
service.Count(Account.EntityLogicalName));
扩展方法:
public static int Count(this IOrganizationService service, string entityLogicalName)
{
string fetch = "<fetch distinct='true' mapping='logical' aggregate='true'>";
fetch += string.Format("<entity name='{0}'>", entityLogicalName);
fetch += "<attribute name='createdon' alias='count' aggregate='count' />";
fetch += "</entity></fetch>";
int count = -1;
try
{
var result = service.RetrieveMultiple(new FetchExpression(fetch));
count = (Int32)((AliasedValue)result.Entities.First()["count"]).Value;
}
catch { }
return count;
}
如果你把FetchXML编辑成Kevin的,它应该工作得很好。
FetchXML只能计数到50000