数据字段 Null CRM 2015 C#

Data field Null CRM 2015 C#

我的代码:

OrganizationServiceProxy service = OrganizationServiceProxy();
            QueryExpression qe = new QueryExpression();
            qe.EntityName = "account";
            qe.ColumnSet = new ColumnSet(new string[]{"accountid","name"});
            DataTable table=new DataTable();
            table.Columns.AddRange(new DataColumn[] { new DataColumn("accountid"), new DataColumn("name") });
            var Compte = service.RetrieveMultiple(qe).Entities.ToList();
            foreach (var item in Compte)
            {                    table.Rows.Add(item.Attributes["accountid"].ToString(), item.Attributes["name"].ToString());
            }
            comboBox1.DataSource = table;
            comboBox1.ValueMember = "accountid";
            comboBox1.DisplayMember = "name";

我的数据是这样的:

         +---------------------------------------+---------------+
                    accountid                          name
         +---------------------------------------+---------------+
            87906183-dbbb-4754-afc9-f2cfcab4942d    Lois Wright
            86a9c978-e1dc-40e2-98cd-6b463ded0f2d      Null
            8e242301-6c97-4509-9031-7237c1d7b14e    Wanda Torres
            2704b13c-8900-4216-98ce-6b03a056ed32      Null
            55f0db83-d4a7-426a-ac7a-a113118b9a1f    Howard Woods
         +---------------------------------------+---------------+

我收到错误 "The given key was not in the dictionary crm"

您的示例中有两条记录的 "name" 字段具有 "null"。当您查询 CRM 时,它只有 returns 属性具有数据。在这种情况下,您需要在尝试使用之前检查响应以确保它包含名称属性:

if (item.Attributes.Contains("name")) {...}

您还可以使用 GetAttributeValue 方法,如果属性不存在,该方法将 return null 而不是抛出异常:

var name = item.GetAttributeValue<string>("name");

https://msdn.microsoft.com/en-us/library/gg326129.aspx