2009-04-07 8 views
2

私はテスト中の文章とちょっと混乱します。私はエンティティへの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()); 
      } 

     } 

答えて

2

、あなたは明示的にコレクションの関連付けをロードする必要があります。痛み。

0

Transparent Lazy Loading for Entity Frameworkをご覧ください。これにより、元のコードジェネレータが新しいコードジェネレータに置き換えられます。新しいものは、ボンネットの下のローディングを処理するエンティティを生成します。コードは完璧ではないことに注意してください - いくつかの問題があります。最大の問題はデータバインディングの問題ですが、ソースを入手するために修正プログラムを簡単に追加できます。

関連する問題