I design LINQ to SQL results for strongly typed classes: parent and child. The performance difference between the two queries is large:
Slow request - a record from the DataContext shows that a separate db call is made for each parent
var q = from p in parenttable select new Parent() { id = p.id, Children = (from c in childtable where c.parentid = p.id select c).ToList() } return q.ToList()
Quick request - logging from a DataContext shows a single request to a remote db that returns all the necessary data
var q = from p in parenttable select new Parent() { id = p.id, Children = from c in childtable where c.parentid = p.id select c } return q.ToList()
I want to force LINQ to use the single request style of the second example, but directly populate the parent classes with its child objects. otherwise, the Children property is the IQuerierable<Child> that must be requested to open the Child object.
Related questions do not seem to affect my situation. using db.LoadOptions does not work. perhaps this requires the type to be TEntity registered in the DataContext.
DataLoadOptions options = new DataLoadOptions(); options.LoadWith<Parent>(p => p.Children); db.LoadOptions = options;
Please note: parents and children are simple types, not Table<TEntity> . and there is no contextual relationship between parent and child. subqueries are ad-hoc.
The key is the problem: in the second LINQ example, I implement IQueriable operators and do not call the ToList() function, and for some reason LINQ knows how to create one single query that can receive all the necessary data. How to fill my advertising projection with actual data, as it was done in the first request? Also, if someone can help me better talk about my question, I would appreciate it.
source share