I am trying to use Entity Framework 6 to get a collection of Person entities, that contains a lazy loaded collection of TimeTrack entities. I only want to include the TimeTrack entities, where the Start property is in a specified period. The code below does what i want, but is not effective.
private async Task<List<Person>> GetPersonsWithTimetracksForPeriod(int companyId, DateTime from, DateTime to)
{
// Repository.AsQueryable() gets context.AsQueryable() with the wanted type
var query = Repository.AsQueryable().Where(e => e.CompanyId == companyId).Include(p => p.TimeTracks);
var persons = await query.ToListAsync();
// Another way of filtering TimeTracks is needed
foreach (var person in persons)
{
person.TimeTracks = person.TimeTracks.Where(t => t.Start >= from && t.Start <= to).ToList();
}
return persons;
}
Is there any way to filter the TimeTracks in the query?