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

Microsoft Dynamics CRM 2016, Business Unit ID Error

我正在尝试从设备查询业务单位 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
}

请哪位帮忙 提前致谢

您必须循环读取 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
        }
                }
            }

如果调试代码,您会发现 businessunitid 属性是 EntityReference,而不是 Guid。这样做:

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