최적화 LINQ SQL 쿼리
-
09-06-2019 - |
문제
가 쿼리에는 다음과 같습니다.
public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
{
DatabaseDataContext db = new DatabaseDataContext();
return (from o in db.Orders
orderby o.CreatedDate descending
select o)
.Skip(pageIndex * recordCount)
.Take(recordCount)
.ToList();
}
나는 필요를 인쇄하는 정보의 순서 및 사용자가 만든다:
foreach (var o in FetchLatestOrders(0, 10))
{
Console.WriteLine("{0} {1}", o.Code, o.Customer.Name);
}
이 생성 SQL 쿼리를 가지고 주문 및 중 하나에 대한 쿼리를 각각의 순서를 가지고 고객입니다.그것은 가능한 쿼리를 최적화하 제공하도록 명령하고 그것의 고객에 SQL 쿼리?
감사
업데이트:에 의해 제안의 sirrocco 를 변경했는데도 다음과 같이 쿼 및 작동합니다.하나만 선택하는 쿼리가 생성됩니다.
public IList<Post> FetchLatestOrders(int pageIndex, int recordCount)
{
var options = new DataLoadOptions();
options.LoadWith<Post>(o => o.Customer);
using (var db = new DatabaseDataContext())
{
db.LoadOptions = options;
return (from o in db.Orders
orderby o.CreatedDate descending
select o)
.Skip(pageIndex * recordCount)
.Take(recordCount)
.ToList();
}
}
감사 sirrocco.
해결책
다른 뭔가를 당신이 할 수 있는 EagerLoading.에 Linq2SQL 사용할 수 있습 LoadOptions: 에 더 LoadOptions 하나는 매우 이상한 일에 대해 L2S 은 당신이 설정할 수 있습니다 LoadOptions 만하기 전에 처음 쿼리를 전송하여 데이터베이스입니다.
다른 팁
를 찾고 싶을 수도 있습니다로 사용하여 컴파일된 쿼리
제휴하지 않습니다 StackOverflow