The case: Since I am using Automapper, I execute the query before OData will do it's filtering. To prevent using more includes than necessary, I want to Include it only when it's present in the $expand parameter of OData.
So far I can only find how to filter inside an .Include(), but not how to filter on using an .Include() at all.
The problem: It doesn't include the Addresses.
The code:
var locations = context.Locations;
if (oDataQueryParameters["$expand"].Contains("Addresses"))
{
locations.Include(l => l.Addresses);
}
locations.Where(l => l.CustomerId == userModel.CustomerId)
.AsNoTracking()
.ToList();