Filter on prefetch path

Posts   
 
    
Subodh
User
Posts: 9
Joined: 20-Nov-2007
# Posted on: 04-Dec-2007 05:32:36   

How to apply predicate expression in prefetch path

I have code like

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FAQCategoryEntity);
 IPrefetchPathElement2 prefetchpathElement = prefetchPath.Add(FAQCategoryEntity.PrefetchPathFAQSubjects, 0, null, null, subjectSorter);
            prefetchpathElement.SubPath.Add(FAQSubjectEntity.PrefetchPathFAQChildSubjects).SubPath.Add(FAQSubjectEntity.PrefetchPathFAQQuestions);

The first level prefetch path fetches the FAQCategoryEntity. And the second level fetched its related FAQSubjectEntity.

I want result as when my FAQCategoryEntity.ID = 4 it should fetch only FAQSubjectEntity with id 14 and 15 (where available subjects for category no. 4 are 14,15,16,17). And when my FAQCategoryEntity.ID = 5 it should fetch only FAQSubjectEntity with id 24 and 25 (where available subjects for category no. 5 are 24,25,26,27).

How to achieve this?

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 04-Dec-2007 07:21:44   

Hi Subodh,

First of all, I think you have an error in this code:

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FAQCategoryEntity);
IPrefetchPathElement2 prefetchpathElement = prefetchPath.Add(FAQCategoryEntity.PrefetchPathFAQSubjects, 0, null, null, subjectSorter);
prefetchpathElement.SubPath.Add(FAQSubjectEntity.PrefetchPathFAQChildSubjects).SubPath.Add(FAQSubjectEntity.PrefetchPathFAQQuestions);

at the last SubPath.Add. I think you meant:

IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FAQCategoryEntity);

IPrefetchPathElement2 prefetchpathElement = prefetchPath.Add(FAQCategoryEntity.PrefetchPathFAQSubjects, 0, null, null, subjectSorter);

prefetchpathElement.SubPath.Add(FAQSubjectEntity.PrefetchPathFAQChildSubjects);
prefetchpathElement.SubPath.Add(FAQSubjectEntity.PrefetchPathFAQQuestions);

Then, if you include the _IPredicateExpression _and _IRelationCollection _when you add the PrefetchPathFAQSubjects, you will obtain the expected results :

// seting the filter for the sub-prefetch path
IPredicateExpression subPathFilter = new PredicateExpression();
subPathFilter.Add( 
     (FAQCategoryEntity.ID == 4 & (FAQSubjectEntity.ID  == 14 | FAQSubjectEntity.ID  == 15))
     | (FAQCategoryEntity.ID == 5 & (FAQSubjectEntity.ID  == 24 | FAQSubjectEntity.ID  == 25))  );

// setting the additional relations for the sub-prefetch path so we would be able to include fields of others entities in the predicate. 
IRelationCollection subPathAdditionalRelations = new RelationCollection();
subPathAdditionalRelations.Add(FAQSubjectEntity.Relations.FAQCategoryEntityUsingFAQCategoryId);

// when add the subPath, pass the filter and relations
IPrefetchPath2 prefetchPath = new PrefetchPath2((int)EntityType.FAQCategoryEntity);
prefetchPath.Add(FAQCategoryEntity.PrefetchPathFAQSubjects, 0, subPathFilter, subPathAdditionalRelations, subjectSorter);

...

I hope that was helpful simple_smile

David Elizondo | LLBLGen Support Team