I had a MockDataStore that returns a List<Task>. Then I would use the Aggregate extension method to query that list. So I was doing a LINQ-to-Objects query which does the job quiet nicely. Now that I’ve replaced my mock data store with a class that inherits the DbContext class, the same query does not work anymore because I cannot use a lambda expression that has a body since it cannot be converted to an expression tree. So is there a way to get around it and still have the aggregation done in SQL and avoid having to do it in-memory? Below is my code:
//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;
}
Why exactly are you using
Aggregate()? I think yourAggregate()call is equivalent to this call toSelectMany():