All of the answers so far are assuming that you're summing up loan.Fees
. But the code you actually posted calls Items.Add()
to add each Item
in loan.Fees.Items
to an Items
object, and it's that Items
object (and not loan.Fees
, which is also an Items
object) that you say you want to sum up.
Now, if Items
is just a simple collection class, then there's no need to do anything other than what people are suggesting here. But if there's some side-effect of the Add
method that we don't know about (or, worse, that you don't know about), simply summing up a filtered list of Item
objects might not give you the results you're looking for.
You could still use Linq:
foreach (Loan.Fee.Item currentFee in loan.Item.Fees.Where(x => x.Classification == 806)
{
class806.Add(currentFee);
}
return class806.Sum(x => x.Fee)
I'll confess that I'm a little perplexed by the class hierarchy implied here, though, in which the Loan.Item.Fees
property is a collection of Loan.Fee.Item
objects. I don't know if what I'm seeing is a namespace hierarchy that conflicts with a class hierarchy, or if you're using nested classes, or what. I know I don't like it.