Microsoft Dynamics CRM 2016,业务单位ID错误

时间:2016-04-27 10:42:49

标签: dynamics-crm-2011 dynamics-crm crm dynamics-crm-2016

我正在尝试从设备查询业务单位ID,但我收到错误

QueryExpression qe = new QueryExpression();
qe.EntityName = "equipment";
qe.ColumnSet = new ColumnSet();
qe.ColumnSet.AddColumns("equipmentid", "name", "businessunitid");
qe.Criteria.AddCondition("name", ConditionOperator.Equal,EquipmentDropdown.Text);
EntityCollection resultequipments = _orgService.RetrieveMultiple(qe);
if(resultequipments.Entities.Count > 0)
{
   Equipmentid = (Guid)resultequipments.Entities[0]["equipmentid"];
   Buid = (Guid)resultequipments.Entities[0]["businessunitid"]; // error at this line
}

有人请帮忙 提前致谢

2 个答案:

答案 0 :(得分:1)

如果您调试代码,您将看到businessunitid属性是EntityReference,而不是Guid。这样做:

var entityRef = (EntityReference)resultequipments.Entities[0]["businessunitid"];
var Buid = entityRef.Id;

答案 1 :(得分:0)

你必须创建一个循环来读取entityCollection

        ConditionExpression condition = new ConditionExpression();
        condition.AttributeName = "name";
        condition.Operator = ConditionOperator.NotNull;
        condition.Values.Add(EquipmentDropdown.Text);

        ColumnSet columns = new ColumnSet("equipmentid", "name", "businessunitid");

        QueryExpression qe = new QueryExpression();
        qe.EntityName = "equipment";
        qe.ColumnSet = columns;

        EntityCollection resultequipments = _serviceProxy.RetrieveMultiple(qe);
        foreach(Entity r in resultequipments.Entities)
        {
                Console.WriteLine( r.Attributes["equipmentid"]);
                Console.WriteLine(r.Attributes["businessunitid"]); // error at this line
        }
                }
            }