有条件地从列表中获取总和 [英] Conditionally get the sum from a list

查看:72
本文介绍了有条件地从列表中获取总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类PropertyDetails:

I have a class PropertyDetails:

public class PropertyDetails 
{      
    public int Sequence { get; set; } 

    public int Length { get; set; }   

    public string Type { get; set; } 
} 

我创建的PropertyDetails列表为

I am creating a list of PropertyDetails as

List<PropertyDetails> propertyDetailsList = new List<PropertyDetails>();

我要从列表中的Length之和,其中PropertyDetails.Sequence< sumValue = 4

I want the sum of Length from the list where PropertyDetails.Sequence < sumValue=4

欢迎使用Linq解决方案.

Linq solutions are welcome.

推荐答案

序列小于4的长度的总和:

Sum of the Lengths where the Sequence is less than 4:

 var result = propertyDetailsList.Where(d => d.Sequence < 4)
                                 .Sum(d => d.Length);

这篇关于有条件地从列表中获取总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆