Regarding the first request: this is a deferred execution. You created IEnumerable from Train s, noticed that it does not have an Include method, so add it to IQueryable , add Include and add ToList() to prevent lazy loading.
But according to MSDN in DbExtensions.Include :
This extension method calls the Include (String) method of the original IQueryable, if one exists. If the source IQueryable does not have a matching method, then this method does nothing.
(my emphasis)
The result of the selection is IEnumerable , converted to IQueryable , but now implemented by EnumerableQuery , which does not implement Include . And nothing happens.
Now the data gets into the grid, which tries to display the station, which starts lazy loading when the context is gone.
In addition, this project has another drawback: it runs a query for each identifier separately.
So the second query is much better. This is one request including Station s. But now the order is dictated by the order that the database wants to return. You can use Concat to solve this problem:
IQueryable<Train> qbase = context.Trains.Include(x=>x.Station); IQueryable<Train> q = null; foreach (var id in ids) { var id1 = id;
The generated request is not very elegant (at least), but in the end it is one request, not many.
source share