Sorry for the confusion. I will be a little more explicate. Remember, I’m only posting an untested example of what I’m trying to do. If you would like the actual code, or a fully tested example I will email it to you because I would rather not post it on this site.
Here is what I have:
Table 1 Contact:
ContactID int (PK)
Name string
CreatedDate datetime
Relation: Contact.ContactID (1:n) Company.ContactID
Table 2 Company:
CompanyID int(PK)
ContactID int
Name string
CreatedDate datetime
Relation: Company.ContactID (m:1) Contact.ContactID
In this example, there can be multiple company rows with different company names pointing to the same contact row. What I would like to do is retrieve contacts filtered on 2 different companies, and prefetch both companies into the Contact.Company EntityCollection.
So, After I filter and prefetch the Companies like so:
// Create the search bucket
RelationPredicateBucket bucket = new RelationPredicateBucket;
bucket.Relations.Add(ContactEntity.Relations.CompanyEntityUsingContactID);
bucket.PredicateExpression.Add(new PredicateExpression( CompanyFields.CompanyID == 5))
bucket.PredicateExpression.Add(new PredicateExpression (CompanyFields.CompanyID == 6))
// Create the prefetch path filtering out CompanyIDs = 5 and 6
PrefetchPath2 fetchContact = new PrefetchPath2((int)ContactEntity));
RelationPredicateBucket bucket = new RelationPredicateBucket();
bucket.Relations.Add(Contact.Relations.CompanyEntityUsingContactID);
bucket.PredicatExpression.Add(FieldCompareValuePredicate(CompanyFields.CompanyID, null, ComparisonOperator.Equal, 5));
bucket.PredicatExpression.Add(new FieldCompareValuePredicate(CompanyFields.CompanyID, null, ComparisonOperator.Equal, 6, "c1"));
fetchContact.Add(ContactEntity.PrefetchPathCompany, 0, bucket.PredicateExpression, bucket.Relations);
// FetchEntityCollection …
This code will produce the following SQL during the prefetch:
Select [Company].*
From
Contact
Inner Join [Company]
ON [Contact].ContactID = [Company].ContactiD
Inner Join Company c1
ON [Contact].ContactID = c1.ContactID
Where [Company].CompanyID = 5 AND c1.CompanyID = 6
Here is where the problem is. The prefetch query only returns the CompanyEntities where the CompanyID = 5 from the non aliased Company table. I need it to return entities from both Company tables where the CompanyID = 5 from the non aliased table, and the CompanyID = 6 for "c1" Company table. If it were possible to add the same prefetch path twice using an alias for the prefetch path, I would be able to return both company entitites in the Contact.Company EntityCollection. However, this is not supported by LLBLGen.
Now if I don’t add the predicate expression to the prefetch path, I will get all of the companies that have the contactID of the contact it is related to, however I only want to prefetch the companies that I’m filtering on. I also need to make sure I’m searching the company table separately for each companyID that is being searched on, so I need to use aliases. I hope this makes a little more sense, let me know if I need to email you some more code, or create a fully tested example. Thanks, I appreciate the help.