Steve Schlichter
Steve Schlichter

Reputation: 57

How to include two collections of a collection using Linq

I have a DBSet, it has a collection that I want to include. that collection has two objects that I want to include. If I use .ThenInclude on the first then I can't seem to get a reference to be able to include the second.

var a = _context.ModelA.Include(x => x.CollectionB).ThenInclude(x => x.ObjectC)

Where can I put a .Include(x => x.ObjectD) // which also belongs to CollectionB

Upvotes: 0

Views: 174

Answers (1)

Darkk L
Darkk L

Reputation: 1035

Multiple inclusion does not exist in EFCore, You have to do it like this

var a = _context.ModelA
.Include(x => x.CollectionB).ThenInclude(x => x.ObjectC)
.Include(x => x.CollectionB).ThenInclude(x => x.ObjectD)

Upvotes: 2

Related Questions