Hi,
I have 2 tables OrderDetail and OrderDetailDiscount corresponding to the lines of order and discounts to be applied by order line.
the final price (after discounts) must be computed like this :
order line price = 100
discount 1 = 5%
discount 2 = 3%
=> final discount = 100 * (1 - 5%) * ( 1 - 3%) = 100 * 0.95 * 0.995
In SQL, I have been able to write it like this :
SELECT order_detail.Id, order_detail.price *
(
SELECT EXP(SUM(LOG(1 - Discount))) AS Discount
FROM order_detail_discount
WHERE order_detail_discount.FK_order_detail_Id = order_detail.Id
GROUP BY order_detail_discount.FK_order_detail_Id
) FinalDiscount
FROM order_detail
WHERE order_detail.Id = XXX
Note : The **Exp(Sum(Log(X))) ** is used to be able to multiply all discount values found by order detail between each other. See http://blogs.x2line.com/al/articles/151.aspx
But I'm not able to write as a Linq query
var query = from orderDetail in metaData.OrderDetail
where orderDetail.Id == XXX
select new OrderDetailProjection
{
Id = orderDetail.Id,
DiscountedPrice =
(from orderDetailDiscount in metaData.OrderDetailDiscount
where orderDetailDiscount.FkOrderDetailId == [b]orderDetail.Id[/b]
group orderDetailDiscount by orderDetailDiscount.FkOrderDetailId into orderDetailDiscountGroup
select Math.Exp(orderDetailDiscountGroup.Sum(o => (1 - Math.Log(o.Discount))))).Single(),
};
=> LLBLGEN throws an exception because of the order_detail.Id that seems to not be put at the right position.
How can I write this query?
Thanks in advance