实体框架导航属性中2列的乘积之和+另外两列的乘积

问题描述 投票:0回答:1

我正在尝试编写一条语句以获取实体框架中两个字段的乘积之和。

给出以下结构:

public class Order
{
    public int OrderNUmber {get; set;}
    public virtual List<Orderline> OrderLines {get; set;}
    public virtual List<ServiceLine> ServiceLines {get; set;}
    public string someproperty {get; set;}
}

public class OrderLine
{
    public string productname {get; set;}
    public int quantity {get; set;}
    public decimal price {get; set;}
}

public class ServiceLine
{
    public string servicename {get; set;}
    public int quantity {get; set;}
    public decimal rate {get; set;}
}

我正在尝试通过一个查询返回总订单价值:

var GrandTotal = Orders.Where(q => q.someproperty == "somecondition")
                 .Sum(order =>
                           order.OrderLines.Sum(line => line.quantity * line.price) 
                         + order.ServiceLines.Sum(sl =>sl.quantity * sl.rate));

但是此版本的确会得到正确的总数。该数字比预期的少。

c# asp.net-mvc-5 entity-framework-6 linq-to-entities
1个回答
0
投票

所以这里的问题是,对于没有任何服务线的任何订单,EF都得到一个空值,而不是添加零。

这两个选项均起作用:

 .Sum(
                order =>
                       order.OrderLines.Select(n => new{n.quantity, n.price}).DefaultIfEmpty(new {quantity = 0, price = decimal.Zero}).Sum(line => line.quantity * line.price) + order.ServiceLines.Select(n => new{n.quantity, n.rate}).DefaultIfEmpty(new {quantity = 0, rate = decimal.Zero}).Sum(acl =>acl.quantity * acl.rate) 
                );

.Sum(
                order =>
                    order.OrderLines.Select(lines => new { LineTotal = lines.quantity * lines.price }).DefaultIfEmpty(new { LineTotal = Decimal.Zero }).Sum(x => x.LineTotal) + order.ServiceLines.Select(acl => new { AclTotal = acl.quantity * acl.rate }).DefaultIfEmpty(new { AclTotal = Decimal.Zero }).Sum(x => x.AclTotal)
            );

必须告诉EF匿名对象的DefaultIfEmpty值,否则它将用null破坏加法。因此,EF将获得OrderLineTotal(值)+ ServiceLineTotal(NULL)= NULL。

© www.soinside.com 2019 - 2024. All rights reserved.