linq - Sum of items in a collection -
using linq sql, have order class collection of orderdetails. order details has property called linetotal gets qnty x itemprice.
i know how new linq query of database find order total, have collection of orderdetails db, there simple method return sum of linetotal directly collection?
i'd add order total property of order class. imagine loop through collection , calculate sum each order.orderdetail, i'm guessing there better way.
you can linq objects , use linq calculate totals:
decimal sumlinetotal = (from od in orderdetailscollection select od.linetotal).sum();
you can use lambda-expressions this, bit "cleaner".
decimal sumlinetotal = orderdetailscollection.sum(od => od.linetotal);
you can hook order-class if want:
public partial class order { ... public decimal linetotal { { return orderdetailscollection.sum(od => od.linetotal); } } }
Comments
Post a Comment