Issue with expressing query in LLBLGen Filter PredicateExpression

Posts   
 
    
jbliss1234
User
Posts: 42
Joined: 04-May-2007
# Posted on: 18-Aug-2010 23:50:25   

I am having trouble expressing a query in the LLBLGen Predicate System.

I am able to express the same query in Linq (Linqdatasource coupled with LLBLGen LinqMetaData), however, the LinqDataSource coupled with LLBLGen meta data does not do updates...

Can you please help me translate this query to LLBLGen filter mechanism?

The Linq query is:

protected void LinqDataSource1_Selecting(object sender, LinqDataSourceSelectEventArgs e)
    {
        LinqMetaData metaData = new LinqMetaData();

        var q = from c in metaData.FeeRecord
                where !(c.ParentRecordId.HasValue)
                && (
                      (c.StatusId == 7) 
                         || 
                      (c.StatusId == 10 && c.AmountToBeTraded == c.ChildrenRecords.Where(c1 => c1.StatusId == 7).Sum(c1 => c1.AmountToBeTraded))
                   )
                orderby c.Plan.Name 
                select c;

        e.Result = q;               
    }

In the above query, if statusid is 10, then we want to make sure that the sum of all the children's amount whose status id is 7 should equate to the parent's amount.

Here is what I have so far:

        RelationCollection relationsToUse = new RelationCollection();
        relationsToUse.Add(FeeRecordEntity.Relations.FeeRecordEntityUsingParentRecordId, "ChildrenFeeRecords");
        relationsToUse.ObeyWeakRelations = true;

        PredicateExpression filter = new PredicateExpression();
        filter.Add(
                                      (FeeRecordFields.StatusId == new List<int>() { 7 }) 
                                                        |
                                      (FeeRecordFields.StatusId == new List<int>() { 10 }
                                      & FeeRecordFields.StatusId.SetObjectAlias("ChildrenFeeRecords") == 7
                                      & FeeRecordFields.AmountToBeTraded == FeeRecordFields.AmountToBeTraded.SetObjectAlias("ChildrenFeeRecords").SetAggregateFunction(AggregateFunction.Sum))
                                                        );
        filter.Add(FeeRecordFields.ParentRecordId == DBNull.Value);

However, the above filter query does not work. The entire bit about children's sum being equal to parent's amount is being ignored.

Using LLBLGen version 2.6.9.616, ASP.Net version 3.5.

daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 19-Aug-2010 06:46:58   

For that you need to use a ScalarQueryExpression. Please see this for an example: http://www.llblgen.com/TinyForum/Messages.aspx?ThreadID=12038

David Elizondo | LLBLGen Support Team
jbliss1234
User
Posts: 42
Joined: 04-May-2007
# Posted on: 23-Aug-2010 21:03:19   

I am not having much success with the ScalarQueryExpression:

Here is what I have so far:

RelationCollection relationsToUse = new RelationCollection();
        relationsToUse.Add(FeeRecordEntity.Relations.FeeRecordEntityUsingParentRecordId, "ChildrenFeeRecords");
        relationsToUse.ObeyWeakRelations = true;

        ScalarQueryExpression childrenAmount = new ScalarQueryExpression(
                FeeRecordFields.AmountToBeTraded.SetObjectAlias("ChildrenFeeRecords").SetAggregateFunction(AggregateFunction.Sum),
                FeeRecordFields.ParentRecordId.SetObjectAlias("ChildrenFeeRecords") == FeeRecordFields.Id &
                FeeRecordFields.StatusId.SetObjectAlias("ChildrenFeeRecords") == 7);

        filter.Add(
                                      (FeeRecordFields.StatusId == new List<int>() { 7 }) 
                                                        |
                                      (FeeRecordFields.StatusId == new List<int>() { 10 }
                                      & FeeRecordFields.AmountToBeTraded == childrenAmount 
                                      )
                                                        );

The exception I get is:

An aggregate may not appear in the WHERE clause unless it is in a subquery contained in a HAVING clause or a select list, and the column being aggregated is an outer reference. 

Here is the fully working Linq query:


var q = from c in metaData.FeeRecord
                where !(c.ParentRecordId.HasValue)
                && (
                     (c.StatusId == 7) 
                         || 
                     (c.StatusId == 10 && c.AmountToBeTraded == c.ChildrenRecords.Where(c1 => c1.StatusId == 7).Sum(c1 => c1.AmountToBeTraded))
                 )
                select c;


daelmo avatar
daelmo
Support Team
Posts: 8245
Joined: 28-Nov-2005
# Posted on: 24-Aug-2010 05:57:04   

Weird,

I perfectly can do this:

EntityCollection<PurchaseOrderEntity> orders = new EntityCollection<PurchaseOrderEntity>();
IRelationPredicateBucket filter = new RelationPredicateBucket();

// Parent.SubTotal equals to SUM of Childre.LineTotal
ScalarQueryExpression lineTotalSumExp = new ScalarQueryExpression(  
    PurchaseOrderDetailFields.LineTotal.SetAggregateFunction(AggregateFunction.Sum),
    PurchaseOrderDetailFields.PurchaseOrderId == PurchaseOrderFields.PurchaseOrderId);

filter.PredicateExpression.Add(PurchaseOrderFields.SubTotal == lineTotalSumExp);

// fetch
using (DataAccessAdapter adapter = new DataAccessAdapter())
{
    adapter.FetchEntityCollection(orders, filter, null);
}

to reproduce this:

SELECT * 
FROM AdventureWorks.Purchasing.PurchaseOrderHeader poh
WHERE SubTotal =
    (SELECT SUM(LineTotal) FROM AdventureWorks.Purchasing.PurchaseOrderDetail pod
     WHERE pod.PurchaseOrderID = poh.PurchaseOrderID
    )

So, please post the full exception message and stack trace. Also post the generated sql, and your code snippet from start to end.

It's also recommended you update to the latest runtime library version (yours are a little bit old).

David Elizondo | LLBLGen Support Team