13

I have the following: List<OutputRow> which contains a number of OutputRow objects.

I am wondering if there is a way for me to use a lambda function on the list to return the total sum of the values of a certain propertyX on each OutputRow object in the list.

Example list:

OutputRow.propertyX = 4  
OutputRow.propertyX = 6  
OutputRow.propertyX = 5  

return 15

Baxter
  • 5,633
  • 24
  • 69
  • 105

2 Answers2

28

Test data

var ls=new List<OutputRow>();
ls.Add(new OutputRow(){propertyX=4});
ls.Add(new OutputRow(){propertyX=6});
ls.Add(new OutputRow(){propertyX=5});

Lambda

var total= ls.Sum(x=>x.propertyX);
Arion
  • 31,011
  • 10
  • 70
  • 88
3

SOmething like this:

var yourSum = yourOutputRowList.Sum(x => x.propertyX);
Huske
  • 9,186
  • 2
  • 36
  • 53