1

I have two lists of objects that I need to join on Sku and then caluclate the Sum (Qty * Price). I'm not exactly sure how to do this? Here is what I have so far:

List<Order> orders = new List <Order> 
{ 
    new Order() { Sku = "ABC", Qty = 1 },
    new Order() { Sku = "XYZ", Qty = 2}
};

List<Cost> costs = new List <Cost> 
{ 
    new Cost() { Sku = "ABC", Price = 4.50m },
    new Cost() { Sku = "XYZ", Price = 2.25m }
};


var profit = from order in orders
             join cost in costs
             on order.Sku equals cost.Sku
             select Sum(order.Qty * cost.Price)
Ori Nachum
  • 588
  • 3
  • 19
PixelPaul
  • 2,609
  • 4
  • 39
  • 70

1 Answers1

5

Change your last part as follows:

var profit = (from order in orders
             join cost in costs
             on order.Sku equals cost.Sku
             select (order.Qty * cost.Price)).Sum();

Taken from here: Stackoverflow question that answered how to sum

Community
  • 1
  • 1
Ori Nachum
  • 588
  • 3
  • 19