Collection based on both 1-N and N-M relations

Posts   
 
    
kbjorn
User
Posts: 13
Joined: 10-May-2005
# Posted on: 03-Oct-2005 14:09:20   

I've got this one solved by merging to collections together, but I'm curious whether there's actually a built-in feature in LLBLGenPro that solves my problem.

I've got three tables:

  1. Comment
  2. CommentSentToUser
  3. User

A Comment is given in by a User and sent to one or more other Users (reflected in the CommentSentToUser table).

I can get an EntityCollection from LLBLGen (selfservicing scenario 2003.NET) based on either the Comment.UserID OR the CommentSentToUser.UserID, but can I easily build a collection based on both properties?

This does it in SQL:

SELECT ... FROM Comment WHERE Comment.UserID = 123 OR Comment.CommentID IN (SELECT CommentID FROM CommentSentToUser WHERE CommentSentToUser.UserID = 123)

Please advice... :-)

Walaa avatar
Walaa
Support Team
Posts: 14995
Joined: 21-Aug-2005
# Posted on: 03-Oct-2005 15:47:33   

Please use the FieldCompareSetPredicate function for the inner select and use AddWithOr function to add this predicate to the Expression you are formulating with the first filter. The following is a code example that you may follow.

IPredicateExpression filter = new PredicateExpression();
                filter.Add(PredicateFactory.CompareValue(CommentFieldIndex.UserID, ComparisonOperator.Equal, 123));
                filter.AddWithOr(
                           new FieldCompareSetPredicate(EntityFieldFactory.Create(CommentFieldIndex.CommentID), 
                           null, 
                           EntityFieldFactory.Create(CommentSentToUserFieldIndex.CommentID),
                           null, 
                           SetOperator.In, 
                           PredicateFactory.CompareValue(CommentSentToUserFieldIndex.UserID,ComparisonOperator.Equal, 123), 
                           false);