嗨,我正在尝试从CRM 2011中获取帐户。我正在获取EntityCollection中的数据。但是,当我尝试从entityCollection中读取或访问数据时,它显示了第一条记录,但在该记录之后引发了错误。请看下面的代码并建议我。
string fetch2 = @"
<fetch version='1.0' output-format='xml-platform' mapping='logical' distinct='false'>
<entity name='account'>
<attribute name='name' />
<attribute name='address1_city' />
<attribute name='primarycontactid' />
<attribute name='telephone1' />
<attribute name='accountid' />
<order attribute='name' descending='false' />
<filter type='and'>
<condition attribute='accounttype' operator='eq' value='01' />
</filter>
</entity>
</fetch>";
try
{
EntityCollection fxResult = _service.RetrieveMultiple(new FetchExpression(fetch2));
foreach (var e in fxResult.Entities)
{
Console.WriteLine("Id:{0},Name:{1},City:{2}", e.Attributes ["accountid"].ToString(), e.Attributes["name"].ToString(), e.Attributes["address1_city"].ToString());
// Console.WriteLine("Id:{0},Name:{1},City:{2}", e.ToEntity["accountid"]);
}
}
catch (Exception e)
{
Console.WriteLine("Error:==" + e.Message);
}
在访问属性之前,您需要询问它是否在上下文中:
e.Attributes.Contains("address1_city")
如果集合包含该属性,则可以安全地访问它。
string accountid = (string)e.Attributes["address1_city"]
该属性未出现在集合中的原因是因为它为null或您没有在检索它。在这种情况下,您的属性之一为空。也许是address1_city。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句