0

I have a parent entity that has two collections of children entities attached to it, lets say a car that has a collection of MOT Inspections and a collection of Services.

I know to get the full set of information it would be something along the lines of...

context
.Cars
.Include(m => m.Mots)
.Include(s => s.Services)
.Where(w => w.Reg == reg)
.FirstOrDefault()

However if I wanted to get the car and only the most recent MOT and most recent Service I can't figure out how to write the lamda.

I spent quite a bit if time Googling, but can't find anything EF Core related on this.

Thanks

Tom

Tom
  • 119
  • 1
  • 7
  • 16
  • Possible duplicate of [EF: Include with where clause](https://stackoverflow.com/questions/16798796/ef-include-with-where-clause) – Gert Arnold Oct 21 '18 at 17:02
  • 1
    And there are many more, just look for "filtered include". It's not supported yet and it's not on EF-core's road map to do that any time soon, although it's a [popular change request](https://github.com/aspnet/EntityFrameworkCore/issues/1833). – Gert Arnold Oct 21 '18 at 17:04
  • @GertArnold Thanks for your reply. Just to confirm, does the relationship fixup work under EF Core also? It seems I'm left with two options, query everything and filter the results afterwards, or perform three queries attaching the results of the 2nd and 3rd to the first. – Tom Oct 21 '18 at 21:14
  • Yes it does. You can use the same strategy as in the duplicate. – Gert Arnold Oct 21 '18 at 21:17
  • @GertArnold Thanks again. One further quick question, in your opinion, for what I'm trying to do (sticking with the example above as it's pretty close) is it better to use the solution in the duplicate post, filter the returned list to the most recent only, or not to get the children in the query and instead query them afterwards where I would be able to use a where clause? – Tom Oct 22 '18 at 06:34

0 Answers0