C# CRM 2011:获取错误
我在crm 2011中有一个包含多个提取的脚本出错。。。错误在于密钥存在并来自:C# CRM 2011:获取错误,c#,dynamics-crm-2011,dynamics-crm,crm,fetchxml,C#,Dynamics Crm 2011,Dynamics Crm,Crm,Fetchxml,我在crm 2011中有一个包含多个提取的脚本出错。。。错误在于密钥存在并来自: <condition attribute='bc_type' operator='eq' lable='Credit' value='948110001' /> 守则: string value_sum = string.Format(@" <fetch distinct='false' mapping='logical' aggregate='true
<condition attribute='bc_type' operator='eq' lable='Credit' value='948110001' />
守则:
string value_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='bc_llbalance'>
<attribute name='bc_units' alias='ded_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='bc_learninglicense' operator='eq' value='{0}' uiname='' uitype='' />
<condition attribute='bc_type' operator='eq' lable='Deduction' value='948110000' />
</filter>
</entity>
</fetch>", a);
EntityCollection value_sum_result = service.RetrieveMultiple(new FetchExpression(value_sum));
foreach (var b in value_sum_result.Entities)
{
TotalDed = ((Decimal)((AliasedValue)b["ded_sum"]).Value);
}
string cre_sum = string.Format(@"
<fetch distinct='false' mapping='logical' aggregate='true'>
<entity name='bc_llbalance'>
<attribute name='bc_units' alias='cre_sum' aggregate='sum' />
<filter type='and'>
<condition attribute='bc_type' operator='eq' lable='Credit' value='948110001' />
<condition attribute='bc_learninglicense' operator='eq' value='{0}' uiname='' uitype='' />
</filter>
</entity>
</fetch>", a);
EntityCollection cre_sum_result = service.RetrieveMultiple(new FetchExpression(cre_sum));
foreach (var c in cre_sum_result.Entities)
{
TotalCre = ((Decimal)((AliasedValue)c["cre_sum"]).Value);
}
string value\u sum=string.Format(@)
“,a);
EntityCollection value_sum_result=service.RetrieveMultiple(新的FetchExpression(value_sum));
foreach(值为var b,值为u sum\u结果实体)
{
总计=((十进制)((别名值)b[“ded_sum”)。值);
}
string cre_sum=string.Format(@)
“,a);
EntityCollection cre_sum_result=service.RetrieveMultiple(新的FetchExpression(cre_sum));
foreach(cre_sum_result.Entities中的var c)
{
TotalCre=((十进制)((别名值)c[“cre_sum”)。值);
}
谢谢:)看起来有个打字错误:
operator='eq' lable='Credit'
^^^^^
应该是
operator='eq' label='Credit'
^^^^^
尝试更改您的
foreach
循环
foreach (var c in cre_sum_result.Entities)
{
if(c.Attributes.ContainsKey("cre_sum"))
{
TotalCre += ((Decimal)((AliasedValue)c["cre_sum"]).Value);
}
}
在尝试强制转换之前,需要测试该值是否存在。您得到的错误是一个通用的.Net错误()
如果在特定记录的字段中找不到值,CRM将不包括该属性,因此该属性将在集合中丢失
您还使用了=
,而不是+=
。这意味着总数是最后一条记录的值,而不是总和
考虑以下几点:
var ListOfNumbers = new List<int> { 1, 2, 3 ,4 }
var total = 0;
foreach (var c in ListOfNumbers)
{
total = c;
}
Console.WriteLine(total.ToString());
将在foreach循环中输出
10
,您必须检查别名cre\u sum和ded\u sum是否包含任何值
比如说,
TotalCre=c.Attributes.Contains(“cre_sum”)?((十进制)((别名值)c[“cre_sum”)。值:0
因此,它将检查它是否包含任何值,如果是,它将返回sum,否则返回0
对两个循环都执行此操作
希望这有帮助 你们是对的,我有,但它似乎并没有改变任何作为有价值的作品,我现在刚刚删除。感谢您指出这一点:)如果我有两个条件都匹配的记录,那么它就可以工作了,但有时可能只有一种记录类型。感谢您的工作非常好:)foreach(cre_sum_result.Entities中的var c){if(c.Attributes.ContainsKey(“cre_sum”){TotalCre+=((十进制)((别名值)c[“cre_sum”])。Value); } }
var ListOfNumbers = new List<int> { 1, 2, 3 ,4 }
var total = 0;
foreach (var c in ListOfNumbers)
{
total = c;
}
Console.WriteLine(total.ToString());
var ListOfNumbers = new List<int> { 1, 2, 3 ,4 }
var total = 0;
foreach (var c in ListOfNumbers)
{
total += c;
}
Console.WriteLine(total.ToString());