私はテスト中の文章とちょっと混乱します。私はエンティティへのLINQで作業中ですLINQはアソシエーション時にフィルタリングできますが、直接参照すると0の結果が返されます
私は以下のコードでコメントしました。私が理解していない最後のConsole.WriteLineです。私はこの
using(Ecommerce.Data.Entities.EcommerceEntities ents = new Ecommerce.Data.Entities.EcommerceEntities())
{
//This returns 1 item as expected
var items = from a in ents.Product
where a.ProductVarianceOption.Count() > 0
select a;
foreach (var item in items)
{
//This outputs AT07 as expected
Console.WriteLine(item.ProductCode);
//This outputs 0 , but because I was able to query
//only those which had a count greater than 0 above
//I do not know why this is returning 0
Console.WriteLine(item.ProductVarianceOption.Count());
}
}
TIA
アンドリュー
UPDATEにanyones入力に感謝:他に だけで包み、誰もがこのに実行され、マルクGravellのおかげで、ここでのソリューションです。 Marc Gravellのコメントは、IsLoadedプロパティとLoadメソッドを探すよう促しました。ありがとう:あなたがオブジェクトをマテリアライズしている場合は、内容を見ることができる前に、エンティティフレームワークと
using(Ecommerce.Data.Entities.EcommerceEntities ents = new Ecommerce.Data.Entities.EcommerceEntities())
{
//This returns 1 item as expected
var items = from a in ents.Product
where a.ProductVarianceOption.Count() > 0
select a;
foreach (var item in items)
{
//This outputs AT07 as expected
Console.WriteLine(item.ProductCode);
//This outputs 0 , but because I was able to query
//only those which had a value greater than 0 above
//I do not know why this is returning 0
Console.WriteLine(item.ProductVarianceOption.Count());
//Load in the data required
item.ProductVariance.Load();
//Load in the data required
item.ProductVarianceOption.Load();
//This now outputs 2. ... As Expected. Thanks to Marc Gravell. :-)
Console.WriteLine(item.ProductVarianceOption.Count());
}
}