计算linq查询中的总计

时间:2015-08-27 00:02:25

标签: c# linq

我有一个名为JobReport的模型,看起来像这样(简化)

public class JobReport
{
    public JobReport()   
    {
        WorkOrders = new List<WorkOrder>();
    }
    public int JobID { get; set; }
    public decimal WorkOrderTotal {get; set; }
    public List<WorkOrder> WorkOrders{ get; set; }
}

public class WorkOrder
{
    public WorkOrder()   
    {
        Total = 0;
    }
    public string Trade { get; set; }
    public int WorkOrderID { get; set; }
    public decimal? Total { get; set; }
}

我现在有了一个Linq查询,它通过Linq Query where related entity contains value from array获取了所有具有交易的WorkOrders的作业,这些交易位于传递的数组中:

jobs = jobs
.Where(x => x.WorkOrders.Any(y => trades.Contains(y.Trade)));

我现在如何获得WorkOrderTotal,它是工作符中满足上述查询谓词的Total的总和?我无法在任何地方看到如何添加.Sum()?

修改

为了确认,每项工作都需要在给定交易中的工作人员总和。

2 个答案:

答案 0 :(得分:1)

尝试这样的事情:

    IEnumerable<decimal> workOrderTotals = jobs
        .Where(x => x.WorkOrders.Any(y => trades.Contains(y.Trade)))
        .Select( j => j.WorkOrders.Sum(wo => wo.Total ?? 0)); 

这是一个测试用例:

  var jobs = new List<JobReport>();
        jobs.Add(new JobReport{ WorkOrders = new List<WorkOrder>{ new WorkOrder{ Total = 10} }});
        jobs.Add(new JobReport { WorkOrders = new List<WorkOrder> { new WorkOrder { Total = 10 }, new WorkOrder { Total = 10 } } });

结果是一个可枚举的,包含2个值10,20

答案 1 :(得分:1)

将此视为测试数据

        JobReport job1 = new JobReport();
        job1.JobID = 1;
        job1.WorkOrders.Add(new WorkOrder() { WorkOrderID = 2, Trade = "trade1", Total = 10});
        job1.WorkOrders.Add(new WorkOrder() { WorkOrderID = 3, Trade = "trade2", Total = 20 });
        job1.WorkOrders.Add(new WorkOrder() { WorkOrderID = 4, Trade = "trade1", Total = 25 });

        JobReport job2 = new JobReport();
        job2.JobID = 2;
        job2.WorkOrders.Add(new WorkOrder() { WorkOrderID = 1, Trade = "trade1", Total = 10 });
        job2.WorkOrders.Add(new WorkOrder() { WorkOrderID = 5, Trade = "trade2", Total = 20 });
        job2.WorkOrders.Add(new WorkOrder() { WorkOrderID = 6, Trade = "trade2", Total = 30 });
        job2.WorkOrders.Add(new WorkOrder() { WorkOrderID = 7, Trade = "trade3", Total = 10 });

        List<JobReport> jobs = new List<JobReport>();
        jobs.Add(job1);
        jobs.Add(job2);

你可以这样做。

var groupedJobs = jobs.GroupBy(a => a.JobID)
            .Select(b => new { JobId = b.Key, WorkOrdersByTrade = b.Select(c => c.WorkOrders.GroupBy(d => d.Trade)
            .Select(g => new { Trade = g.Key, tradeSum = g.Sum(s => s.Total) })) });

进一步定义以下类

    public class TradeTotal
    {
        public string Trade { get; set; }
        public decimal? Total { get; set; }
    }

    public class JobTrade
    {
        public int JobId { get; set; }
        public List<TradeTotal> TradeTotals { get; set; }
    }

您可以使用您想要的格式获得结果

var JobTradeList = groupedJobs.Select(x => new JobTrade() { JobId = x.JobId, TradeTotals = x.WorkOrdersByTrade.SelectMany(s => s.Select(v => new TradeTotal() { Total = v.tradeSum, Trade = v.Trade })).ToList() }).ToList();

代码可能不是100%干净;但我认为这就是你所追求的目标。

enter image description here