25

これは機能するコードです。

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).(Include"Contexts.AdditionalProperties.Field");

ただし、「Contexts.AdditionalProperties.Field」の文字列ステートメントを間違えた場合、コンパイル時エラーが発生することはありません。

以下にコードを書きたいと思います。

IQueryable<Product> productQuery = ctx.Set<Product>().Where(p => p.Id == id).Include(p => p.Contexts);

しかし、上記のステートメントでは、AdditionalPropertiesとFieldを定義する機会を与えることができませんでした。

私たちは何をすべき?

ビルドクエリ用に複数のインクルードとして書きたいと思います。

ありがとう。

4

1 に答える 1

43

AdditionalPropertiesが別のオブジェクトへの単一の参照である場合:

using System.Data.Entity;
...
IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Field)
        .Where(p => p.Id == id);


AdditionalPropertiesがコレクションの場合、 Selectメソッドを使用できます。

IQueryable<Product> productQuery = ctx.Set<Product>()
        .Include(p => p.Contexts.AdditionalProperties.Select(a => a.Field))
        .Where(p => p.Id == id);

クラスファイルにSystem.Data.Entity名前空間をインポートすることを忘れないでください!

于 2011-01-20T19:06:26.953 に答える