给定的键在字典中不存在 [英] The given key is not present in the dictionary

查看:85
本文介绍了给定的键在字典中不存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从CRM 2011中获取帐户。我正在获取EntityCollection中的数据。但是,当我尝试从entityCollection中读取或访问数据时,它显示了第一条记录,但在该记录之后引发了错误。

Hi I am trying to fetch the accounts from CRM 2011. I am fetching the data in the EntityCollection . But when I am trying to read or access data from entityCollection it displayed first record but throwing an error after that record. Kindly have a look to below code and suggest me.

  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);
        }


推荐答案

在访问属性之前,您需要询问是否在上下文中:

Before access an attribute you need to ask if it is in the context:

e.Attributes.Contains("address1_city")

如果集合包含该属性,则可以安全地访问它。

If the collection contains the attribute, then you can access it safe.

string accountid = (string)e.Attributes["address1_city"]

该属性未出现在集合中的原因是因为它为null或您没有在检索它。在这种情况下,您的属性之一为空。也许是address1_city。

The reason the attribute doesn't come in the collection it's because it is null or you are not retrieving it. In this case maybe, one of your attributes is null. Maybe address1_city.

这篇关于给定的键在字典中不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆