次のエンティティ フレームワーク モデルがあります。
class Farm{
string owner;
List<Animal> animals;
DateTime StartDate;
}
class Animal{
string Name;
DateTime DOB;
}
問題:
開始日が >= 2013/01/01である農場のコレクションとその動物を選択したいと考えていますが、DOB >= 2013/06/01 でフィルター処理されています。
私は次のことを試しました:
試行 1 :
//This still shows all animals from each farm, if there is at least one
//animal with the required DOB
var x = context.Farm.Where(y => y.StartDate >= myDate
&& y.Animal.Any(z => z.DOB >= otherDate)
).Include("Animal");
試行 2 :
//I subclassed the Farm class because i cant instantiate the class
//from Entity Framework directly, and that should be my return type.
class Temp:Farm{}
var x = context.Farm.Where(y => y.StartDate >= myDate).Include("Animal")
.Select(z => new Temp(){
owner = z.owner,
animals = new TrackableCollection<Animal>(){ z.animals.Where(y => y.DOB >= newDate).SingleOrDefault() });
//Couple of things here:
//1: I instantiated a new TrackableCollection because thats what the collection
//type of Animal is inside Entity Framework.
//2: This still doesnt work for some reason, if i use this approach, the list
//of animals in the farm comes with 0 elements.
トライ3:
これを読んだ後: Ef-query-with-conditional-include
var x = (from farm in ctx.Farm
from animal in farm.Animal
where animal.DOB => newDate
select new{farm, animal}).AsEnumerable().Select(x=> x.farm).Distinct().ToList();
//I have no idea how this works, but it does...
上記の仕組みを説明したい人はいますか?
基本的に、クエリは親エンティティと必要なパラメーターによってフィルター処理された子エンティティを選択します。エンティティ フレームワークは、「リレーションシップ フィックスアップ」を介して、選択された子が選択された親に関連付けられていることを認識しているため、親コレクションにも追加されます。ハックな解決策のように見えますが、実際に機能します。
--アンドレイ・D.