Convert EntityReference to Entity

前端 未结 3 1105
夕颜
夕颜 2021-02-04 09:47

Does anyone know how can Convert EntityReference to Entity.

protected override void Execute(CodeActivityContext execution         


        
3条回答
  •  醉梦人生
    2021-02-04 10:30

    An EntityReference is just the logicalName, name, and id of the entity. So to get an Entity, you just need to create the entity using the properties of the EntityReference.

    Here is an Extension Method that performs that for you:

    public static Entity GetEntity(this EntityReference e)
    {
        return new Entity(e.LogicalName) { Id = e.Id };
    }
    

    Don't forget that none of the other attributes of the entity will be populated. If you want the attributes you'll need to query for them:

    public static Entity GetEntity(this IOrganizationService service, EntityReference e)
    {
        return service.Retrieve(e.LogicalName, e.Id, new ColumnSet(true));
    }
    

    And if you like @Konrad's Field answer, make it a params array and it is nicer to call

    public static Entity GetEntity(this IOrganizationService service, EntityReference e, 
       params String[] fields)
    {
        return service.Retrieve(e.LogicalName, e.Id, new ColumnSet(fields));
    }
    

提交回复
热议问题