2013-03-07 2 views
10

エンティティへのEntityReferenceの変換方法がわかっていますか。EntityReferenceをエンティティに変換する

protected override void Execute(CodeActivityContext executionContext) 
{ 
    [Input("Email")] 
    [ReferenceTarget("email")] 
    public InArgument<Entity> EMail { get; set; } 


    Entity MyEmail = EMail.Get<Entity>(executionContext); 

これは私にエラーが発生します。これを変換できません。

答えて

13

ご質問への最短の答えは、実体参照によって指摘だエンティティ(と呼ばれる)のためのデータベースを照会することです。私はいつもエンティティ参照をC++のポインタと同等(ラフ)と見てきました。それはそれにアドレスを持っている(guid)が、あなたはそれを蜂蜜に得るために参照を解除する必要があります。あなたはこのようにします。

IOrganizationService organization = ...; 
EntityReference reference = ...; 

Entity entity = organization.Retrieve(reference.LogicalName, reference.Id, 
    new ColumnSet("field_1", "field_2", ..., "field_z")); 

私はエンティティのEntityReferenceからの変換の多くを行うとき、私はフィールドのオプションのパラメータを拡張メソッドを展開します。

public static Entity ActualEntity(this EntityReference reference, 
    IOrganizationService organization, String[] fields = null) 
{ 
    if (fields == null) 
    return organization.Retrieve(reference.LogicalName, reference.Id, 
     new ColumnSet(true)); 
    return organization.Retrieve(reference.LogicalName, reference.Id, 
    new ColumnSet(fields)); 
} 

あなたはより多くを読み、EntityReferenceEntityを比較することができます。

13

EntityReferenceは、エンティティの論理名、名前、およびIDです。したがって、Entityを取得するには、EntityReferenceのプロパティを使用してエンティティを作成するだけです。ここで

はあなたのためにそれを実行している拡張メソッドです:

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

は、エンティティの他の属性のいずれも取り込まれないことを忘れないでください。

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

をそして、あなたはコンラートのフィールドの答え@好きなら、それparams配列にすると、

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

エンティティを呼び出すことよりよいです:あなたが属性をしたい場合、あなたは彼らのために照会する必要がありますEntityReferenceは異なります。 EntityReferenceは、GUIDとエンティティの論理名を含むレコードの参照です。エンティティにはGUIDと論理名でアクセスする必要があります。そのような何か:

service.Retrieve(logicalname, guid, new ColumnSet(columns)); 
関連する問題