1

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

有人请帮助提前谢谢

4

2 回答 2

1

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

var entityRef = (EntityReference)resultequipments.Entities[0]["businessunitid"];
var Buid = entityRef.Id;
于 2016-04-27T12:23:26.450 回答
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
        }
                }
            }
于 2016-04-27T11:04:19.787 回答