how to?

Posts   
 
    
billb
User
Posts: 50
Joined: 09-Jul-2004
# Posted on: 10-Oct-2005 14:55:28   

I apologize if this has been asked many times, but I can't seem to find the solution.

My query is this: SELECT * FROM Rating INNER JOIN ClassCodes ON Rating.ClassCodeID = ClassCodes.ClassCodeID AND Rating.ClassCodeTableID = ClassCodes.ClassCodeTableID

I'm having difficulty trying to figure out how to do the JOIN with the AND as shown above.

Thanks for the help.

Walaa avatar
Walaa
Support Team
Posts: 14950
Joined: 21-Aug-2005
# Posted on: 10-Oct-2005 16:32:10   

Use the CustomeFilter inside the IEntityRelation.

"Custom filter for JOIN clauses which are added with AND to the ON clause resulting from this EntityRelation. By adding a predicate expression with fieldcomparevalue predicate objects for example, you can add extra filtering inside the JOIN"

billb
User
Posts: 50
Joined: 09-Jul-2004
# Posted on: 10-Oct-2005 17:02:11   

Walaa wrote:

Use the CustomeFilter inside the IEntityRelation.

"Custom filter for JOIN clauses which are added with AND to the ON clause resulting from this EntityRelation. By adding a predicate expression with fieldcomparevalue predicate objects for example, you can add extra filtering inside the JOIN"

I stumbled on to custom filter shortly after I posted the message. However, I'm still having some trouble. I receive the error: Invalid cast from ClassCodesFieldIndex to System.Guid. I assume this is because I need to give it a value and not a field? How do I specify the value?

Thanks again.


                IPredicateExpression customFilter = new PredicateExpression();
                customFilter.Add(PredicateFactory.CompareValue(RatingFieldIndex.ClassCodeTableId, ComparisonOperator.Equal, ClassCodesFieldIndex.ClassCodeTableId));
                IRelationPredicateBucket filter = new RelationPredicateBucket();

                filter.Relations.Add(RatingEntity.Relations.ClassCodeEntityUsingClassCodeId).CustomFilter = customFilter;

                ratings = new EntityCollection(new RatingEntityFactory());

                adapter.FetchEntityCollection(ratings, filter);

Paul.Lewis
User
Posts: 147
Joined: 22-Aug-2005
# Posted on: 11-Oct-2005 04:44:58   

Billb,

You're almost there. I made one tweak to your predicateExpression referencing a variable myClassCodeTableID that should contain the specific ID/GUID value you are joining against.



IPredicateExpression customFilter = new PredicateExpression();
customFilter.Add(PredicateFactory.CompareValue(RatingFieldIndex.ClassCodeTableId, ComparisonOperator.Equal, myClassCodeTableID));
IRelationPredicateBucket filter = new RelationPredicateBucket();

filter.Relations.Add(RatingEntity.Relations.ClassCodeEntityUsingClassCodeId).CustomFilter = customFilter;

ratings = new EntityCollection(new RatingEntityFactory());
adapter.FetchEntityCollection(ratings, filter);


I didn't compile it, but the rest looked good to me.

billb
User
Posts: 50
Joined: 09-Jul-2004
# Posted on: 12-Oct-2005 13:55:32   

Sorry for being thick, but what is the value of myClassCodeTableID? I'm just doing a join, I don't have a particular value. The value for ClassCodeTableID is coming from whatever is in the Rating table since its a column in that table. Is your solution still applicable?

Thanks again!

Paul.Lewis wrote:

Billb,

You're almost there. I made one tweak to your predicateExpression referencing a variable myClassCodeTableID that should contain the specific ID/GUID value you are joining against.



IPredicateExpression customFilter = new PredicateExpression();
customFilter.Add(PredicateFactory.CompareValue(RatingFieldIndex.ClassCodeTableId, ComparisonOperator.Equal, myClassCodeTableID));
IRelationPredicateBucket filter = new RelationPredicateBucket();

filter.Relations.Add(RatingEntity.Relations.ClassCodeEntityUsingClassCodeId).CustomFilter = customFilter;

ratings = new EntityCollection(new RatingEntityFactory());
adapter.FetchEntityCollection(ratings, filter);


I didn't compile it, but the rest looked good to me.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39614
Joined: 17-Aug-2003
# Posted on: 12-Oct-2005 14:07:16   

If you want to compare 2 columns, you have to use a FieldCompareExpressionPredicate, created by PredicateFactory.CompareExpression. You can with that, create a filter like: WHERE Foo = Bar

where both foo and bar are fields in the same table (or another table in the join).

If you use a FieldCompareValuePredicate (created by PredicateFactory.CompareValue), the value passed in is considered a value, and you can create a filter like: WHERE Foo = @value

so if you pass in a field index, like MyTableFieldIndex.Bar, it will consider that as a value, not as a field. So your code should look like:


IPredicateExpression customFilter = new PredicateExpression();
customFilter.Add(PredicateFactory.CompareExpression(
    RatingFieldIndex.ClassCodeTableId, ComparisonOperator.Equal, 
        new Expression (EntityFieldFactory.Create(ClassCodesFieldIndex.ClassCodeTableId))));

IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.Relations.Add(RatingEntity.Relations.ClassCodeEntityUsingClassCodeId).CustomFilter = customFilter;
ratings = new EntityCollection(new RatingEntityFactory());
adapter.FetchEntityCollection(ratings, filter);

Frans Bouma | Lead developer LLBLGen Pro
billb
User
Posts: 50
Joined: 09-Jul-2004
# Posted on: 14-Oct-2005 14:33:35   

I did a bit more jiggling, since I had some other tables to add to the mix and I believe I got it to work. Very cool. Thanks guys!

Otis wrote:

If you want to compare 2 columns, you have to use a FieldCompareExpressionPredicate, created by PredicateFactory.CompareExpression. You can with that, create a filter like: WHERE Foo = Bar

where both foo and bar are fields in the same table (or another table in the join).

If you use a FieldCompareValuePredicate (created by PredicateFactory.CompareValue), the value passed in is considered a value, and you can create a filter like: WHERE Foo = @value

so if you pass in a field index, like MyTableFieldIndex.Bar, it will consider that as a value, not as a field. So your code should look like:


IPredicateExpression customFilter = new PredicateExpression();
customFilter.Add(PredicateFactory.CompareExpression(
    RatingFieldIndex.ClassCodeTableId, ComparisonOperator.Equal, 
        new Expression (EntityFieldFactory.Create(ClassCodesFieldIndex.ClassCodeTableId))));

IRelationPredicateBucket filter = new RelationPredicateBucket();
filter.Relations.Add(RatingEntity.Relations.ClassCodeEntityUsingClassCodeId).CustomFilter = customFilter;
ratings = new EntityCollection(new RatingEntityFactory());
adapter.FetchEntityCollection(ratings, filter);