0

나는 List<Task>을 반환하는 MockDataStore을 가졌습니다. 그런 다음 해당 목록을 쿼리하려면 Aggregate 확장 방법을 사용합니다. 그래서 일을 조용하게 멋지게 처리하는 LINQ-to-Objects 쿼리를 수행했습니다. 이제 모의 데이터 저장소를 DbContext 클래스를 상속하는 클래스로 대체 했으므로 식 트리로 변환 할 수 없기 때문에 본문이있는 람다 식을 사용할 수 없으므로 동일한 쿼리가 더 이상 작동하지 않습니다. 그렇다면 SQL을 사용하여 집계를 수행하고 메모리에서 수행하지 않아도되는 방법이 있습니까?DbContext에 대해 집계 확장 메서드를 사용하는 쿼리

 //List<Task> tasks = MockDataStore.GetData() 
     // .Aggregate(new List<Task>(), (accumulator, treaty) => { accumulator.AddRange(treaty.Tasks); return accumulator; }) 
     // .Where(x => x.AssignedTo.Equals(companyId) && x.StatusId == statusId).ToList(); 

     using (SCGREDbContext context = new SCGREDbContext()) 
     { 
      List<Task> tasks = context.Treaties.Aggregate(new List<Task>(), (accumulator, treaty) => { accumulator.AddRange(treaty.Tasks); return accumulator; }) 
           .Where(x => x.AssignedTo.Equals(companyId) && x.StatusId == statusId).ToList(); 
      return tasks; 
     } 

답변

2

왜 정확히 Aggregate()를 사용 : 다음은 내 코드는? 나는 당신의 Aggregate() 전화가 SelectMany()이 호출하는 것과 같습니다 생각 :

context.Treaties.SelectMany(treaty => treaty.Tasks) 
+0

우수함! 이제 마침내 어떻게'SelectMany'가 사용되는지 이해합니다 :) – Kassem