Entity Framework を使用したプロジェクションの作成について質問があります。
Profile、Biography、BiographyTranslation の 3 つのテーブルがあります。
次の投影を行いたい: (伝記 = IQueryable)
return biographies.Select(b => new ProfileBiography
{
Id = b.BiographieID,
OwnerId = b.Profile.ProfileID,
Translations =
b.Translations.Select(t => new DocumentTranslation()
{
DocumentId = b.BiographieID,
FileName = t.BiographyTextFile,
Title = t.Title,
Text = t.Text,
LanguageId = t.LanguageID
})
}
私の問題: OwnerId は正しく投影されますが、翻訳の数は常に 0 です。
プロジェクションはコレクション プロパティでは機能しませんか?
biographies.Inlcude("Translations") も試してみましたが、結果は同じでした。
アップデート
ここにある小さなテストプロジェクトを作成しました:
3 つのテーブル (create および insert ステートメントを含む)、EDMX ファイル、および伝記をロードするためのクラスのみです。コレクション ナビゲーション プロパティを投影するためにまだ動作させることができません。単純なナビゲーション プロパティは正常に動作します。多分誰かがテストプロジェクトから私が間違っていることを見ることができます....
アップデート
私は新しいアプローチを試みましたが、まだ運がありません:
using (var context = new DatabaseEntities())
{
return context.BiographySets
.Where(bio => bio.ProfileId == profileId)
.Select(bio => new DocumentModel()
{
Id = bio.Id,
OwnerId = bio.Profile.Id,
Translations = context.BiographyTranslationSets.Where(t => t.BiographyId == bio.Id)
.Select(t => new DocumentTranslation()
{
DocumentId = bio.Id,
LanguageId = t.LanguageId,
Text = t.Text
})
}).ToList();
}
アップデート
匿名型を使用すると、驚くほど機能します...
using (var context = new DatabaseEntities())
{
return context.BiographySets
.Where(bio => bio.ProfileId == profileId)
.Select(bio => new
{
Id = bio.Id,
OwnerId = bio.Profile.Id,
Translations = bio.Translations
.Select(t => new
{
DocumentId = bio.Id,
LanguageId = t.LanguageId,
Text = t.Text
})
}).ToList();
}