aggregate functions with fields from different tables

Posts   
 
    
yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 12-Mar-2006 14:42:10   

version 1.0.2005.1 final (self-servicing) VS2005 winforms


hiya,

I have an entityCollection that contains the following fields.

deliveryId PK barcode FK qty productName....a field mapped onto tblProduct.productName.

the other relevant database table is:

tblProduct barCode PK productName unitPrice

I want to grab the SUM total of the delivery. My problem is that "unitPrice" is NOT in the entityCollection.

I'm not sure what I should do?

Can anyone help?

many thanks,

yogi



            TblDeliveryProductsCollection deliveries = new TblDeliveryProductsCollection();

            //yogi: how do I tie the  TblDeliveryProductsFields.Qty   WITH   TblProductFields.UnitPrice??
            IExpression DeliveryTotalExpression = new Expression(TblDeliveryProductsFields.Qty, ExOp.Mul, TblProductFields.UNITPRICE);

            IPredicate filter = PredicateFactory.CompareValue(TblDeliveryProductsFieldIndex.DeliveryId, ComparisonOperator.Equal, 0);

    
             object deliveryTotal = deliveries.GetScalar(TblDeliveryProductsFieldIndex.DeliveryId, DeliveryTotalExpression, AggregateFunction.Sum, filter); 


JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 12-Mar-2006 16:01:57   

You may want to reconsider your db design. Prices do change over time.

The Delivery table should probably record the unitprice AT the time of delivery for posterity.

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 12-Mar-2006 16:18:44   

Ah! I think Prefetch Path could be the answer.

Please have a look at Using Generated Code --> Self Service --> Pre Fetch Path section in th manual.

Note: Jim has a very valid point regarding price changes wink

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 12-Mar-2006 16:25:38   

good point Jim.

I'm not sure how i would implement this though. I have added the unitPrice field to tblDeliveryProducts sqlerver database table.

Now, do I add a "field mapped on related field" ?? I assume I have to use the llblgenPro project to generate this field?

many thanks,

yogi

sparmar2000 avatar
Posts: 341
Joined: 30-Nov-2003
# Posted on: 12-Mar-2006 16:51:42   

yogiberr wrote:

good point Jim.

I'm not sure how i would implement this though. I have added the unitPrice field to tblDeliveryProducts sqlerver database table.

Now, do I add a "field mapped on related field" ?? I assume I have to use the llblgenPro project to generate this field?

many thanks,

yogi

I think Jim was probably thinking of having the unitPrice and qty on the tblDeliveryProducts table and then when you create a delivery row, you populat the unitPrice with the latest price of the product.

P.S Jim please feel free to correct me.

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 12-Mar-2006 16:59:24   

Exactly simple_smile

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 12-Mar-2006 17:04:56   

cheers folks.

I've added the additional field and re generated the code. I obviously have to assign the "current" unitPrice in tblProduct when I am creteing a new delivery entity.

Do I have to create an instance of my "product" entity, so that I can grab the unitPrice?

many thanks,

yogi


TblDeliveryProductsEntity newProduct = new TblDeliveryProductsEntity();         
            newProduct.DeliveryId = 0;
            newProduct.BarCode = txtBarcode.Text;
            newProduct.Qty = 1;
            newProduct.UnitPrice = ?????????
            newProduct.Save();

JimHugh
User
Posts: 191
Joined: 16-Nov-2005
# Posted on: 12-Mar-2006 17:41:29   

Either that or use GetScalar to retrieve just that value, it depends on if you use any other entity properties from the Products table.

yogiberr
User
Posts: 432
Joined: 29-Jun-2005
# Posted on: 13-Mar-2006 13:18:38   

thanks Jim and Sparmar.

I don't use any other fields.I'll just grab an entity and use the property.At this stage, I just need it to work.

ta for all the help. Another post happily closed :-)

yogi