Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 统计订购产品的数量_C#_Dynamics Crm 2011 - Fatal编程技术网

C# 统计订购产品的数量

C# 统计订购产品的数量,c#,dynamics-crm-2011,C#,Dynamics Crm 2011,我正在尝试获取与订单关联的订单产品计数。 这是我的密码: //count the number of lines in the actual order QueryExpression query = new QueryExpression("salesorderdetail"); query.ColumnSet.AddColumns("salesorderid"); query.Criteria = new FilterExpression(); query.Criteria.AddCondi

我正在尝试获取与订单关联的订单产品计数。 这是我的密码:

//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);
由于某些原因,当我使用response.TotalRecordCount时,我将-1作为计数,即使有一些记录。
我猜-1与一个问题有关,表达式中是否有任何错误?

在减少从CRM发送到本地计算机的数据量时,您可以/应该使用FetchXML进行查询。下面是一个示例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编写了一个扩展方法

可以这样称呼:

扩展方法:

public static int Count(此IOR组织服务,字符串entityLogicalName)
{
字符串fetch=“”;
fetch+=string.Format(“,entityLogicalName”);
取数+=“”;
取数+=“”;
整数计数=-1;
尝试
{
var result=service.RetrieveMultiple(新的FetchExpression(fetch));
count=(Int32)((AliasedValue)result.Entities.First()[“count”]).Value;
}
捕获{}
返回计数;
}
如果您将FetchXML编辑到Kevin的,它应该可以正常工作。
FetchXML只能计数到50000。

response.Entities.count()
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;
}