Total sum for an object property value in a list using a lambda function

29,023

Solution 1

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);

Solution 2

SOmething like this:

var yourSum = yourOutputRowList.Sum(x => x.propertyX);
Share:
29,023
Baxter
Author by

Baxter

Updated on July 05, 2022

Comments

  • Baxter
    Baxter almost 2 years

    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