C# 使用 lambda 函数的列表中对象属性值的总和
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10316444/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Total sum for an object property value in a list using a lambda function
提问by Baxter
I have the following: List<OutputRow>which contains a number of OutputRowobjects.
我有以下内容:List<OutputRow>其中包含许多OutputRow对象。
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.
我想知道是否有办法在列表上使用 lambda 函数来返回列表中每个 OutputRow 对象上某个 propertyX 的值的总和。
Example list:
示例列表:
OutputRow.propertyX = 4
OutputRow.propertyX = 6
OutputRow.propertyX = 5
return 15
返回 15
采纳答案by Arion
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);
回答by Husein Roncevic
SOmething like this:
像这样的东西:
var yourSum = yourOutputRowList.Sum(x => x.propertyX);

