I have the following two entities:
public class Person
{
public Person()
{
Items = new HashSet<Item>();
}
public string Id { get; set; }
public string Name { get; set; }
public ICollection<Item> Items { get; private set; }
}
public class Item
{
public string Id { get; set; }
public DateTime Date { get; set; }
public string Status { get; set; }
public Person Person { get; set; }
public string PersonId { get; set; }
}
I would like to select all the persons, and only include their latest Item (ordered by date).
I figured something like this should work:
var persons = _context.Persons
.Include(e => e.Items.OrderByDescending(i => i.Date).Take(1))
.ToList();
But apparently EF Core is unable to do this. The items collection will get very large (> 20000), so loading them all for every person is undesirable. What should I do instead?