I have the following code that I would like to convert the for eaches into one linq statement
decimal originalCharges = 0;
List<int> rdns = new List<int>()
{
1,
2
};
foreach (var customerOrder in list)
{
foreach (CustomerBillTo billTo in customerOrder.BillTos)
{
originalCharges += billTo.Charges
.Where(charge => rdns.Contains(charge.RateCodeRdn))
.Sum(charge => charge.Amount);
}
}
This is what I have tried so far
originalCharges = list.ForEach(co => co.BillTos.ForEach(bt => bt.Charges.Where(charge => rdns.Contains(charge.RateCodeRdn)).Sum(charge => charge.Amount)));
Which is giving me an error cannot convert type "void" to "decimal".
I think I am getting messed up around what to do with the += for originalCharges like I did in the original forEach loops in the first example.
Can this be done in a linq statement?
originalCharges = list.ForEach(co => co.BillTos.ForEach( ));