This question is very similar to my previous question. Use LINQ to count the number of combinations existing in two lists , with the exception of some additional twists.
I have a CartItem list that can receive a discount based on the items specified in the DiscountItem s list. I need to remove items from the basket that can receive a discount and apply the corresponding discount specified in DiscountItem . The discount applies only to every combination that exists. Here's what the two lists look like before the discount is applied:
BEFORE DISCOUNT:
CartItems DiscountItems
==================================================== =======
SKU Qty DiscountApplied SKU DiscountAmount
==================================================== =======
Ham 2 $ 0.00 Ham $ 0.33
Bacon 1 $ 0.00 Bacon $ 2.00
Ham 1 $ 0.00
Bacon 2 $ 0.00
Cheese 1 $ 0.00
Bacon 1 $ 0.00 The hard part is that it's not just a matter of combining two lists or counting the number of combinations. The discount applies to all DiscountItem combinations that appear in the CartItem s list. There are three combinations in the above example, and if you have to apply a discount for three combinations iteratively through the list, the data will look like this every time a discount is applied:
After 1st Discount is applied:
CartItems DiscountItems
==================================================== =======
SKU Qty DiscountApplied SKU DiscountAmount
==================================================== =======
Ham 2 $ 0.33 Ham $ 0.33
Bacon 1 $ 2.00 Bacon $ 2.00
Ham 1 $ 0.00
Bacon 2 $ 0.00
Cheese 1 $ 0.00
Bacon 1 $ 0.00
After 2nd Discount is applied:
CartItems DiscountItems
==================================================== =======
SKU Qty DiscountApplied SKU DiscountAmount
==================================================== =======
Ham 2 $ 0.66 Ham $ 0.33
Bacon 1 $ 2.00 Bacon $ 2.00
Ham 1 $ 0.00
Bacon 2 $ 2.00
Cheese 1 $ 0.00
Bacon 1 $ 0.00
After 3rd Discount is applied:
CartItems DiscountItems
==================================================== =======
SKU Qty DiscountApplied SKU DiscountAmount
==================================================== =======
Ham 2 $ 0.66 Ham $ 0.33
Bacon 1 $ 2.00 Bacon $ 2.00
Ham 1 $ 0.33
Bacon 2 $ 4.00
Cheese 1 $ 0.00
Bacon 1 $ 0.00 In the end, everyone gets a discount, with the exception of cheese and extra bacon. Cheese does not receive a discount because it is not a discount on the list. Extra bacon does not receive a discount because it does not have the corresponding ham subject to qualify for the discount. A total of 3 ham and 4 bacon, so one of the bacon will not receive a discount.
I assume that I will be able to solve this problem with LINQ, since it involves listing more than two separate lists, but I cannot figure out which LINQ methods I will use to make this happen. The end result of a LINQ query should be a CartItem set with a discount applied.
source share