- Home
- LLBLGen Pro
- LLBLGen Pro Runtime Framework
SubPath of Prefetech cannot work in ToList() or AsEntityCollection()
Joined: 28-Dec-2010
Hi
We encountered to problem on get value from memory data by prefetech way.
**Business logic code is : ** public IQueryable<QnTextEntity> LoadQnDetailsDataSource() { var q = FilterByQnCode(); q = QnPrefetchPaths(q); return q; }
private IQueryable<QnTextEntity> FilterByQnCode() { return linqMetaData.QnText.Where(q => q.QnCode == qnCode && q.LanguageCode == languageCode); }
private IQueryable<QnTextEntity> QnPrefetchPaths(IQueryable<QnTextEntity> q) { string reglanguageCode = languageCode; int idClient2 = idClient; string facilityCode2 = facilityCode;
return
q.WithPath(
t =>
t.Prefetch<QnBaseEntity>(b => b.QnBase).SubPath(
s => s.Prefetch<HeadingBaseEntity>(h => h.HeadingBase).SubPath(
d =>
d.Prefetch<HeadingTextEntity>(e => e.HeadingText).FilterOn(
l => l.LanguageCode.Equals(reglanguageCode, StringComparison.InvariantCultureIgnoreCase)))
.Prefetch<ItemsBaseEntity>(
v => v.ItemsBaseCollectionViaQnItems)
.Prefetch<AnswerQnEntity>(
f => f.AnswerQn).FilterOn(
f => f.IDClient == idClient2 && f.FacilityCode.Equals(facilityCode2,
StringComparison.
InvariantCultureIgnoreCase))
.Prefetch<QnItemsEntity>(i => i.QnItems).SubPath(
j => j.Prefetch<ItemsBaseEntity>(k => k.ItemsBase).SubPath(
l => l.Prefetch<ItemsTextEntity>(m => m.ItemsText)))
.Prefetch<RegulationBaseEntity>(r => r.RegulationCollectionViaRegulationQn)
.SubPath(r => r.Prefetch<RegulationTextEntity>(x => x.RegulationText)
.FilterOn(
l =>
l.LanguageCode.Equals(reglanguageCode,
StringComparison.InvariantCultureIgnoreCase))
.Prefetch<RegulationHeadingEntity>(h => h.RegulationHeading))
.Prefetch<RegulationQnEntity>(r => r.RegulationQn).SubPath(
u => u.Prefetch<RegulationQnCitationEntity>(c => c.RegulationQnCitation)
.Prefetch<RegulationBaseEntity>(r => r.Regulation)
.SubPath(
r => r.Prefetch<RegulationTextEntity>(x => x.RegulationText)
.FilterOn(
l =>
l.LanguageCode.Equals(reglanguageCode,
StringComparison.
InvariantCultureIgnoreCase))
.Prefetch<AnswerRegulationEntity>(e => e.AnswerRegulation)
.FilterOn(j =>
j.IDClient == idClient2 &&
j.FacilityCode.Equals(facilityCode2,
StringComparison.
InvariantCultureIgnoreCase))))));
}
public static EntityCollection<T> AsEntityCollection<T>(this IQueryable<T> linqStatement) where T : EntityBase2 { return (EntityCollection<T>) (((ILLBLGenProQuery)linqStatement).Execute<IEntityCollection2>()); }
**Test Code : ** [Test] [Row("NLQ00179", "Belgium-Bruxelles", 322, "2011-1-2")] [Description("Passes can get the LastModifiedDate")] public void Can_GetQnDetailsDatasource_LastModifiedDate_Return_NotNull(string qnCode, string faciltyCode, int idClient, string lastModifiedDate) { fixture.QnDetailsTestFixture(qnCode: qnCode, faciltyCode: faciltyCode, idClient: idClient, answerLastModifiedDate: lastModifiedDate); var qnBase = model.LoadQnDetailsDataSource().AsEntityCollection().First().QnBase; var answerQn = model.LoadQnDetailsDataSource().AsEntityCollection().First().QnBase.AnswerQn.First();
//var answerQn = model.LoadQnDetailsDataSource().ToList().First().QnBase.AnswerQn.First();
var metaData = new LinqMetaData(adapter);
var expect = Convert.ToDateTime(lastModifiedDate);
Assert.AreEqual(expect.Date, answerQn.AnswerLastModifiedDate.Value.Date);
}
My questions here: I only get the first level value on prefetch. Such as qnBase, I can get it normally from ToList() or AsEnitityCollection(). But I cannot get the answerQn which is subPath of qnBase from ToList() or AsEnitityCollection(). I didn't know why?
The strange thing, when I bind this LoadQnDetailsDataSource with llblgenpro datasource on a gridview. I can get all prefetched entity in memory.
The llblgenpro version: SD.LLBLGen.Pro.DQE.SqlServer.NET20.dll V2.6.10.0917 SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll V2.6.10.0809 SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll V2.6.10.0930
Best regards Ricky
I formatted your code a little bit to understand it:
q.WithPath
(
t => t.Prefetch<QnBaseEntity>(b => b.QnBase)
.SubPath
(
s => s
.Prefetch<HeadingBaseEntity>(h => h.HeadingBase)
.SubPath
(
d => d.Prefetch<HeadingTextEntity>(e => e.HeadingText)
.FilterOn(l => l.LanguageCode.Equals(reglanguageCode,
StringComparison.InvariantCultureIgnoreCase))
)
.Prefetch<ItemsBaseEntity>(v => v.ItemsBaseCollectionViaQnItems)
.Prefetch<AnswerQnEntity>(f => f.AnswerQn)
.FilterOn(f => f.IDClient == idClient2 && f.FacilityCode.Equals(facilityCode2,
StringComparison.InvariantCultureIgnoreCase))
.Prefetch<QnItemsEntity>(i => i.QnItems)
.SubPath
(
j => j.Prefetch<ItemsBaseEntity>(k => k.ItemsBase)
.SubPath
(
l => l.Prefetch<ItemsTextEntity>(m => m.ItemsText)
)
)
.Prefetch<RegulationBaseEntity>(r => r.RegulationCollectionViaRegulationQn)
.SubPath
(
r => r
.Prefetch<RegulationTextEntity>(x => x.RegulationText)
.FilterOn(l => l.LanguageCode.Equals(reglanguageCode,
StringComparison.InvariantCultureIgnoreCase))
.Prefetch<RegulationHeadingEntity>(h => h.RegulationHeading)
)
.Prefetch<RegulationQnEntity>(r => r.RegulationQn)
.SubPath
(
u => u
.Prefetch<RegulationQnCitationEntity>(c => c.RegulationQnCitation)
.Prefetch<RegulationBaseEntity>(r => r.Regulation)
.SubPath
(
r => r
.Prefetch<RegulationTextEntity>(x => x.RegulationText)
.FilterOn(l => l.LanguageCode.Equals(reglanguageCode,
StringComparison.InvariantCultureIgnoreCase))
.Prefetch<AnswerRegulationEntity>(e => e.AnswerRegulation)
.FilterOn(j => j.IDClient == idClient2
&& j.FacilityCode.Equals(facilityCode2,
StringComparison.InvariantCultureIgnoreCase))
)
)
)
);
Now this are your tests:
var qnBase = model.LoadQnDetailsDataSource().AsEntityCollection().First().QnBase;
var answerQn = model.LoadQnDetailsDataSource()
.AsEntityCollection().First()
.QnBase.AnswerQn.First();
var answerQn = model.LoadQnDetailsDataSource()
.ToList().First()
.QnBase.AnswerQn.First();
... and if I understand you, you don't get the AnswerQn object in both cases in above code snippet.
I tested similar scenario with AdventureWorks (with a simpler case) and everything is fine. Please examine the generated sql, if your query is ok, the prefetch queries should appear there. Then check the query runs in a query on your DB and returns the expected results for the prefetch paths. Maybe your tested parameters simply doesn't return rows for the prefetchs (i.e. that QnBase tested doesn't have any AnswerQn).
As we don't know your schema and data, we simply try to reproduce it over here with AdventureWorks and similar scenario and code. If you can reproduce it with Northwind or AdventureWorks that would be even better.