ScalarQueryExpression question...

Posts   
 
    
simmotech
User
Posts: 1024
Joined: 01-Feb-2006
# Posted on: 06-Aug-2007 07:58:11   

If I have this code:

            Predicate documentFilter = ShipFields.ID == DocumentTransactionFields.ShipID &
                                       DocumentTransactionFields.VoyageID == DBNull.Value &
                                       DocumentTransactionFields.AllocatedFlag == true;

            ScalarQueryExpression followUpMessagesQuery =
                new ScalarQueryExpression(
                    DocumentFields.ID.SetAggregateFunction(AggregateFunction.Count), 
                    documentFilter & DocumentFields.FollowUpFlag == true, relations);

      ScalarQueryExpression hasPriorityMessagesQuery =
        new ScalarQueryExpression(
          DocumentFields.ID.SetAggregateFunction(AggregateFunction.Count),
          documentFilter & DocumentFields.PriorityFlag == true, relations);

I am performing almost the same query twice. Looking back in the forums, I see that Derived Tables were mentioned some time ago but I can't find anything current. Am I right in thinking this would be a candidate for a Derived Table so I can retrieve two pieces of information from one query?

Secondly, the second expression currently returns an aggregate Count but I actually only need to know if any matching rows are present. Seems a waste to count all the documents (there might be a lot) when ideally I would like it to short-circuit on finding one. This might be more of a SQL question but what is the best way of doing this?

Cheers Simon

DvK
User
Posts: 323
Joined: 22-Mar-2006
# Posted on: 06-Aug-2007 09:23:16   

Can't you use an EXISTS clause for the COUNT ?

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 06-Aug-2007 13:09:46   

Derived tables were cut as they were a pain to define (as in: the API to get them defined was pretty verbose)

The second query indeed should be an exist predicate. So do a getscalar where you fetch the id of the row with an exists filter.

Frans Bouma | Lead developer LLBLGen Pro
arschr
User
Posts: 894
Joined: 14-Dec-2003
# Posted on: 06-Aug-2007 17:18:40   

Derived tables were cut as they were a pain to define

I really hope they will return/appear in 3.0.

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39910
Joined: 17-Aug-2003
# Posted on: 06-Aug-2007 17:32:44   

arschr wrote:

Derived tables were cut as they were a pain to define

I really hope they will return/appear in 3.0.

I know you're waiting on them simple_smile We'll do our best simple_smile

Frans Bouma | Lead developer LLBLGen Pro