2

次の「Foo」および「Bar」エンティティがあるとします。

class Foo {
   int FooId;
   string FooName;
}

class Bar {
   int BarId;
   Foo RelatedFoo;
   string BarName;
}

また、「RelatedFoo」をデフォルトで遅延ロードしたいとします。

Entity Frameworkでは、要素が「bar.RelatedFoo.FooName」でソートされている列挙可能な「Bar」エンティティを返すクエリを実行できますか?

もしそうなら、これは一定数のデータベースクエリで行うことができますか? N+1 クエリを実行することは避けたいと思います。

そうでない場合、これは別の .NET ORM フレームワークで可能ですか?

4

1 に答える 1

1
var bars = _context.Bars.OrderBy(b => b.RealtedFoo.FooName)

RealtedFoonullではないバーのみを戻すこともできます

var bars = _context.Bars.Where(b => b.RelatedFoo != null).OrderBy(b => b.RealtedFoo.FooName)

アップデート:

    //For EF only
    _context.Configuration.LazyLoadingEnabled = false

    //If you want to bring back RealtedFoo then include it. 
//Otherwise, you can just query for it and not use the Include() extension.
    var bars = _context.Bars.Include(b => b.RealtedFoo).Where(b => b.RelatedFoo != null).OrderBy(b => b.RealtedFoo.FooName)
于 2012-12-08T15:30:36.750 に答える