2012-12-08 2 views
2

가정하자 나는이 다음 "푸"와 "바"엔티티 :Entity Framework는 관련 엔터티의 속성별로 엔터티를 정렬 할 수 있습니까?

class Foo { 
    int FooId; 
    string FooName; 
} 

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

의도 나는 "RelatedFoo"을한다고 가정하자 게으른로드 기본적으로 할 수 있습니다.

엔티티 프레임 워크에서

, 그것은 요소 "bar.RelatedFoo.FooName"으로 분류되어 있습니다 "바"개체의 열거를 반환하는 쿼리를 할 수 있습니까?

그렇다면 고정 된 수의 데이터베이스 쿼리에서이를 수행 할 수 있습니까? N+1 queries을 피하고 싶습니다.

그렇지 않은 경우 다른 .NET ORM 프레임 워크에서 가능합니까?

답변

1
var bars = _context.Bars.OrderBy(b => b.RealtedFoo.FooName) 
또한 단지 RealtedFoo가 null가 아닌 그 술집을 돌아 오게 할 수 있습니다

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) 
+0

이 하나의 DB 쿼리 할 것인가? – chrisdfrey

+0

SQL Server에서 실제 쿼리가 어떻게 보이는지 알 수는 없지만 SQL 프로파일 러를 실행하여 볼 수 있습니다. 그게 당신이 요구하는 것입니까? – DDiVita

+0

내가 RelatedFoo에 액세스하여 N 개의 쿼리를 수행하지 않았는지 확인하려고합니다. – chrisdfrey

관련 문제