Linq to SQL ignore loading options

33 Views Asked by At

I use to prefetch data using loading options, so that the communication with the DB do not become too chatty.

Everything works fine, but when I use ExecuteQuery (instead of Linq query), the loading options are just ignored:

context.DeferredLoadingEnabled = false;

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<my_entity>(entity=> entity.ChildEntity1);
options.LoadWith<my_entity>(entity=> entity.ChildEntity2);
options.LoadWith<my_entity>(entity=> entity.ChildEntity3);

context.LoadOptions = options;

string myQuery = "select * from my_entity where property <= {0}";
context.ExecuteQuery<my_entity>(myQuery , queryParameter).ToList();

If I trace what happens on the database, I see that, foreach entity read by myQuery, there are three queries to fetch child entities (ChildEntity1, ChildEntity2, ChildEntity3).

Is there a way to disable deferred loading in this scenario? I also find acceptable to not load child entities at all.

Note: in the example above if I write

context.DeferredLoadingEnabled = false;

DataLoadOptions options = new DataLoadOptions();
options.LoadWith<my_entity>(entity => entity.ChildEntity1);
options.LoadWith<my_entity>(entity => entity.ChildEntity2);
options.LoadWith<my_entity>(entity => entity.ChildEntity3);

context.LoadOptions = options;

context.my_entity.Where (row => row.property < value).ToList();

Everything work as expected (loading options are applied). So seems to me that this is an issue of ExecuteQuery, and I repeat I find it perfectly acceptable that the child entities are not loaded at all, leaving up to me the task to load them.

0

There are 0 best solutions below