【问题标题】:How to find sum of a field in nested lists with a where condition?如何在具有 where 条件的嵌套列表中查找字段的总和?
【发布时间】:2016-11-14 15:35:17
【问题描述】:

我有两个列表,我需要找到嵌套列表的总和,第一个列表上应该有过滤器。

例如:

Class Customer{
    string Name
    List<Order> Orders
    string State
}

Class Order{
    int OrderID
    int OrderTotal
    int ItemCode
}

我需要找到特定状态下的订单总和,我正在为此寻找一个 lambda 表达式。

【问题讨论】:

标签: list lambda nested


【解决方案1】:

下面是 lambda 表达式,可用于获取 orderTotal 与过滤器开启状态的总和。 客户客户=新客户(); 现在向您的客户对象和订单对象添加一些数据。

customer.Where(cust => cust.State.Equals("Alaska")).Sum(order => order.OrderTotal);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-01-17
    • 2023-03-23
    • 2012-08-24
    • 2013-07-01
    • 1970-01-01
    • 2022-06-15
    • 2014-09-22
    相关资源
    最近更新 更多