简体   繁体   English

在EF查询语法中使用let时发生NullReferenceException

[英]NullReferenceException when using let in EF query syntax

I have a query where if I use a let I get a NullReferenceException however if I use the same statement in the select it works fine. 我有一个查询,如果我使用let会得到一个NullReferenceException但是如果我在select中使用相同的语句,它将很好地工作。

var q = db.WorkOrders;

The following query throws an exception: 以下查询引发异常:

(from wo in q.OrderByDescending(x => x.WorkOrderNumber)
let cost = wo.Costs.Select(x => x.BaseCost + x.AdminOverhead + x.LabourOverhead).DefaultIfEmpty(0).Sum()
select new ManageWorkOrderData
{
    Id = wo.Id,
    TotalCost = cost
}).ToListAsync();

However the following does not: 但是,以下内容不是:

(from wo in q
select new ManageWorkOrderData
{
    Id = wo.Id,
    TotalCost = wo.Costs.Select(x => x.BaseCost + x.AdminOverhead + x.LabourOverhead).DefaultIfEmpty(0).Sum()
}).ToListAsync();

Notice that in the second the subquery is the same, just not as a let . 注意,第二个子查询是相同的,只是不如let

Update This question has been marked as a duplicate, however I don't agree as the code works fine when not used in the let part of the linq query. 更新此问题已被标记为重复,但是我不同意,因为在linq查询的let部分中未使用该代码时,该代码可以正常工作。 None of the types are nullable, and it's Entity Framework interpreting the code so I can't check for nulls. 这些类型都不是可为空的,并且它是Entity Framework解释代码的,因此我无法检查null。

There is no way to debug the code in VS as the code is all executed by Entity Framework/SQL. 由于代码全部由Entity Framework / SQL执行,因此无法在VS中调试代码。 Interestingly the query isnt executed on the server, which leads me to believe its an EF issue. 有趣的是,查询未在服务器上执行,这使我相信它是EF问题。 I'm interested to see if anyone else has any insight as to why EF throws an exception with the let and not when used in the select. 我很想看看其他人是否对EF为什么在let引发异常而不是在select中使用引发异常有任何见解。

Classes

public class WorkOrder
{
    public int Id { get; set; } 
    public int WorkOrderNumber { get; set; } 
    public virtual ICollection<Cost> Costs { get; set; }

    public WorkOrder()
    {
        Costs = new HashSet<Cost>();
    }
}

public class Cost
{
    public int Id { get; set; }
    public int WorkOrderId { get; set; }
    public decimal BaseCost { get; set; }
    public decimal AdminOverhead { get; set; }
    public decimal LabourOverhead { get; set; }

    public virtual WorkOrder WorkOrder { get; set; }
}

public class ManageWorkOrderData
{
    public int Id { get; set; }
    public decimal TotalCost { get; set; }
}

Stack Trace Available from http://pastebin.com/uXkdM30A 堆栈跟踪可从http://pastebin.com/uXkdM30A获得

This is an Entity Framework bug. 这是一个实体框架错误。 This can be seen from the stack trace. 这可以从堆栈跟踪中看到。 This is an uncontrolled crash deep inside of EF. 这是EF内部无法控制的崩溃。 Microsoft classes never intentionally throw meaningless programmer error exceptions like NRE, ArrayIndexEx, DivByZeroEx, ... Microsoft类永远不会故意抛出无意义的程序员错误异常,例如NRE,ArrayIndexEx,DivByZeroEx,...

Create a small, executable repro and report the bug on GitHub. 创建一个小型的可执行repro,并在GitHub上报告错误。 The team is responsive. 团队反应迅速。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM