LoadWith not returning entities
Posted: Wed 21 Oct 2015 02:23
I'm not sure if I'm missing something here. We're trialling LinqConnect and are trying a simple operation to return a tree of objects in a single query.
Using the documented method, the following should populate three levels deep:
The where clause works perfectly, and the system performs a valid join in a single query, even correctly filtering on the complex nested where clause (*1*).
But... when the code gets to the nested loop (*2*), the system does a lazy load of all the same entity properties that were returned in the first query!
So, I thought I'd simplify things:
This does a lazy load of every customer object, yet commenting out the QuestionnaireSet line (*3*) avoids the unnecessary lazy loading.
Oh, and adding the where clause from the first code breaks the call as the generated SQL contains a series of 'ORDER BY' clauses that are invalid within the nested selects.
Am I missing something here? Are these bugs, or am I somehow not using the system in the way you expect?
Cheers,
Jason
Using the documented method, the following should populate three levels deep:
Code: Select all
using (var dbc = new DBContext())
{
var customers = dbc.CustomerAccounts
.LoadWith(ca => ca.QuestionnaireSets
.LoadWith(qs => qs.Questionnaires.LoadWith(q => q.QuestionnaireType, FetchMode.Join)
, FetchMode.Join), FetchMode.Join)
(*1*) .Where(ca => !ca.Suspended && ca.QuestionnaireSets.Any(qs => qs.Questionnaires.Any(q => q.QuestionnaireStatusID==3)));
foreach (var c in customers)
{
Console.WriteLine(c.CustomerAccountName);
foreach (var cx in c.QuestionnaireSets)
{
Console.WriteLine(" > " + cx.Name);
(*2*) foreach (Questionnaire q in cx.Questionnaires)
{
Console.WriteLine(" > " + q.QuestionnaireID);
}
}
}
}
But... when the code gets to the nested loop (*2*), the system does a lazy load of all the same entity properties that were returned in the first query!
So, I thought I'd simplify things:
Code: Select all
using (var dbc = new DBContext())
{
var dbo = new DataLoadOptions();
dbo.LoadWith<CustomerAccount>(ca => ca.QuestionnaireSets, FetchMode.Join);
(*3*) dbo.LoadWith<QuestionnaireSet>(qs => qs.Questionnaires, FetchMode.SelectByOne);
dbc.LoadOptions = dbo;
var customers = dbc.CustomerAccounts;
foreach (var c in customers.ToList())
{
Console.WriteLine(c.CustomerAccountName);
}
}
Oh, and adding the where clause from the first code breaks the call as the generated SQL contains a series of 'ORDER BY' clauses that are invalid within the nested selects.
Am I missing something here? Are these bugs, or am I somehow not using the system in the way you expect?
Cheers,
Jason