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

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

给出以下结构:

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));

但是,此版本的点不会得到正确的总数。这个数字比预期的要少。

yymbaya 回答:实体框架导航属性中2列乘积的和+另外两列乘积的和

所以这里的问题是,对于没有任何ServiceLine的任何订单,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) 
                );

AND

.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。

本文链接:https://www.f2er.com/3155849.html

大家都在问