Sum of columns from different tables

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 12-Jan-2006 00:33:29   

VS2005 llblGenPro: 1.0.2005.1 designer version: 1.0.2005.1 adapter: selfServicing

hiya,

I have been browsing thru all the topics, but I'm still a bit stuck.

I need to calculate the entire value of an order.

tblOrder orderId PK productId FK qty

tblProduct productId PK unitPrice

So, I think I have to:

1) filter the orderEntityCollection with the orderId 2) iterate thru each product in tblOrder obtaining the qty (using productId as a fliter?) 3) multiply qty with the corresponding tblProduct.unitPrice 4) SUM all of the values to obtain a grand total

I know that the above is all fairly standard stuff.The thing is, that I have been following the northwindCS example..but then I saw the improved code that Frans posted...

 IExpression expOp1 = ((OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity) -
                    (OrderDetailsFields.UnitPrice * OrderDetailsFields.Quantity * OrderDetailsFields.Discount / 100));
            EntityField2 op1 = OrderDetailsFields.UnitPrice.SetAggregateFunction(AggregateFunction.Sum).SetExpression(expOp1);
            IExpression scalarExpression = op1 / OrderDetailsFields.Quantity.SetAggregateFunction(AggregateFunction.Sum);
etc

When I saw that code, I got a bit more confused, because it seemed to open up even more permutations. Could anyone comment on the best approach for a quick solution..I can tidy it up later :-)

as ever, many thanks,

yogi

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 12-Jan-2006 03:25:12   

I believe this is more confusing because of the discount.

IExpression expOp1 = (OrderItemsFields.UnitPrice * OrderDetailsFields.Quantity); /* This calculates the price for each item in the based on its quantity.*/
EntityField2 op1 = OrderDetailsFields.UnitPrice.SetAggregateFunction(AggregateFunction.Sum).SetExpression(expOp1); /*This should then sum each line item to arrive at a total for the order.*/

Does this help or is there more that you were wondering.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 12-Jan-2006 11:23:59   

hiya,

It was confusing because I was torn between the sample on NorthwindExampleCS (which I imagine uses deprecated code, or at least, it uses code that isn't as desirable as that in the newer example that Frans posted?)

If so, then I ditch the NorthwindExampleCS and simply use the newer code?

cheers, yogi

Otis avatar
Otis
LLBLGen Pro Team
Posts: 39933
Joined: 17-Aug-2003
# Posted on: 12-Jan-2006 12:19:32   

It's my understanding the examples use the new way of formulating filters/expressions confused

Frans Bouma | Lead developer LLBLGen Pro
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 13-Jan-2006 00:54:24   

OK,

I am piecing together the example for my scenario. All I want to do is SUM the value of the items in the Order that has the orderId of "2"


 IExpression expOp1 = (dalDelivery.HelperClasses.TblProductFields.UnitPrice * dalDelivery.HelperClasses.TblDeliveryFields.Qty); //no compile-time error

EntityField2 op1 = TblProductFields.UnitPrice.SetAggregateFunction(AggregateFunction.Sum).SetExpression(expOp1);  //errors, pls see below

<error> 1) A field initializer cannot reference the nonstatic field, method, or property 2) Cannot implicitly convert type 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField' to 'SD.LLBLGen.Pro.ORMSupportClasses.EntityField2' </error>

The learning curve is starting to hurt :-( , but i really appreciate the effort that people make to help :-)

many thanks,

yogi

bclubb
User
Posts: 934
Joined: 12-Feb-2004
# Posted on: 13-Jan-2006 02:55:00   

Are you using selfservicing. This example was for adapter which is why EntityField2 is specified instead of EntityField. Try changing it to this instead

EntityField op1 = TblProductFields.UnitPrice.SetAggregateFunction(AggregateFunction.Sum).SetExpression(expOp1);

Here's an example from the manual if you are still having problems that may help out.

// C#
OrderDetailsCollection orderDetails = new OrderDetailsCollection();
decimal orderPrice = (decimal)orderDetails.GetScalar(OrderDetailsFieldIndex.OrderId, 
    (OrderDetailsFields.Quantity * OrderDetailsFields.UnitPrice), AggregateFunction.Sum, 
    (OrderDetailsFIelds.OrderId == 2));
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 13-Jan-2006 11:15:39   

righto,

yes I am using selfServicing. I will go with the code you kindly posted and grab the stuff from the manual as well.

many thanks,

yogi