Reuse a predicate filter

Posts   
 
    
Rene
User
Posts: 54
Joined: 20-Jun-2006
# Posted on: 05-Jun-2008 17:47:43   

Hi!

Got a question.

I have a resultset with a rather complex relation/predicate bucket. There are multiple subqueries and some of them look the same for the larger part.

Something like this (in pseudo SQL):

SELECT Fields FROM Table1 WHERE Fieldx IN (SELECT Field FROM Table2 AS Alias1 WHERE conditions) AND Fieldx IN (SELECT Field FROM Table2 AS Alias 2 WHERE conditions)

The conditions in both subqueries are the same, except for one or two expressions. So for all the expressions that are the same I would like to make one predicate-filter for all the expressions that are the same in subquery 1 and subquery 2. And then reuse it in the FieldCompareSetPredicate. Problem is that the Table2 has different Aliasses in Subquery 1 and Subquery 2. Is there a way to set an aliase for an entire predicate or set it at the time I add the FieldCompareSetPredicate? Or a different solution?

I use LLBLGen 1.0.2005.1, Adapter, C#, .NET 2.0.

Thanks alot! Rene Lergner, Prodist Software.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 06-Jun-2008 07:20:43   

Hi Rene, I don't think that such way exists. However you can write your own method that receives a _FieldCompareSetPredicate _and a string alias, set the aliases of the objectalias and the aliases of the field on conditions and return the new predicate.

David Elizondo | LLBLGen Support Team
Rene
User
Posts: 54
Joined: 20-Jun-2006
# Posted on: 06-Jun-2008 09:25:45   

Hi David,

Thanks for your response. What you suggest is actually what I'm doing right now. But I thought that there might be a solution where I don't need to create the Predicate many times, but just once and then reuse it. Producing the predicate in a function is a good solution too, but it might have a slight impact on the performance (but I didn't measure it, so I'm not realy sure). Also, reusing the predicate might result in a bit cleaner code. But if that's not possible, I will just keep my code as it is right now.

While going through the docs, finding a way to reuse the predicate (in the way, that I described it) I saw that the constructor of FieldCompareSetPredicate also takes a parameter 'ObjectAlias'. But I don't know how I should use it (and I don't know whether it is even useful for me). Is it an ObjectAlias for the 'field' or for the 'setField'? Is it also applied to the 'filter', 'sorter' and 'groupByClause'? Can someone give a brief example of how it should be used? Because I can't seem to find any examples.

Thanks alot! René

Walaa avatar
Walaa
Support Team
Posts: 14994
Joined: 21-Aug-2005
# Posted on: 06-Jun-2008 10:56:45   

From the comments of the FieldCompareSetPredicate class:

Alias for the object the field belongs to. Used to identify which entity to use when the entity is present multiple times in a relation collection. Alias has to match an alias specified in the relation collection of the query this predicate is part of or should be left empty if no alias is specified (or no relation collection is used in the query this predicate is part of). In that case, use another overload.

In other words if you specified an alias "XYZ" it would only be used as follows:

SELECT anything from someTable WHERE field1 IN (SELECT anotherField from anotherTable XYZ WHERE .......)

So actually you'd need to specify the alias again for the predicates of the inner where clause.

Rene
User
Posts: 54
Joined: 20-Jun-2006
# Posted on: 06-Jun-2008 11:16:25   

Ok! Thanks for the elaboration. I understand now. This actually does not help me with my code, because 'the predicates of the inner where clause' are the predicates I wanted to reuse with different aliasses. But that does not seem to be possible. I'll keep my code the way it is now (like David described it).

Thanks! Always quick support on this site. Thumbs up! René