Reputation: 4928
I have a two tables called "Orders" and "OrderItems" and they have the following columns
OrderId, OrderDate, OrderType,
ItemId, OrderId, Price,
I need to return the OrderId, OrderDate, OrderType and the Sum Of the Price. However I can't seem to find a good example of how to do this in linq to entities.
Can anyone help?
EDIT
Just to be clear, I need show one line for each order with the price being the sum of all the order items for that order.
Upvotes: 0
Views: 47
Reputation: 4423
Let's say there are 3 orders IDs: 1,2,3 and there are 3 order items as mentioned below:
ID=3, OrderID=3 ,Price=14
var carts = (from Cart o in Context.Orders
join oi in Context.OrderItems on o.Id equals oi.OrderId into orderItems
select new
{
o.Id,
o.OrderDate,
o.OrderType,
sum = orderItems.Sum(p=> p.Price)
}).ToList();
The linq mentioned above will select sum as mentioned below
Upvotes: 1
Reputation: 4895
context.Orders.Include(order => order.OrderItems).Select(order => new {
OrderId = order.OrderId,
OrderDate = order.OrderDate,
Ordertype = order.OrderType,
TotalPrice = i.OrderItems.Sum(orderItem => orderItem.Price)
});
Maybe you could try something like this.
Upvotes: 0