How to use .Include() with .Select() in EF Core (.NET Core)


I come across a situation in which I had a navigation property that had a property with a collection of items and I needed to use this data to loop over within a React component. The problem was, I was using a .Select() within LINQ to format my JSON response to a more suitable format that I can easily interpret on my frontend. This then caused the .Include() to “not work” and fail to populate the property.

The Solution

The solution was pretty simple, although I’m still wondering if there is a better way (please comment if you have one). All I did was include the following after my .Include()‘s and before my .Select();

Apparently this causes the query to get executed and then the rest of the operations after will be done in memory. This then populated my navigation property collection property and I was able to use the data on the frontend.

Example code



I hope this helped you out if you’re wondering why your properties aren’t being populated when you use an .Include() and a .Select()!