我已经构建了一个内部应用程序,允许用户查看我们网站上的待处理订单。 99.9%的时间没有问题,但每天有几次我收到错误:
转换为值类型'System.Boolean'失败,因为具体化值为null。
我正在使用计时器执行以下代码,然后WebSockets将其发送给客户端:
var _orders = db.Orders.Where(o =>
!(o.Cancelled ?? false)
&& (o.EvoImport == null || !o.EvoImport.Imported || (o.OrdersChanged.Any() && o.EvoAmendImport != null && !o.EvoAmendImport.Imported) || (o.OrderPrintings.Any() && o.OrderPrintings.Max(p => p.PrintDate) < o.OrderDate))
&& !unpaid.Contains(o.Paid)).GroupJoin(db.CustomerOrders, o => o.BasketID, c => c.BasketID, (o, c) => new { Order = o, Customers = c.DefaultIfEmpty() })
.SelectMany(d => d.Customers.Select(c => new { Order = d.Order, Customer = c })).Take(100);
orders.AddRange(_orders
.Select(o =>
new OrderLine()
{
Amendment = (bool?)o.Order.OrdersChanged.Any() ?? false,
BasketID = o.Order.BasketID ?? -1,
Company = o.Customer.AccountCode,
Completed = o.Order.Complete ?? false,
DeliveryDate = o.Order.CustomerDelivery.FirstOrDefault().EvoDispatchDate.Value,
EmailID = o.Order.OrderEmails.OrderByDescending(e => e.DateAdded).FirstOrDefault().EmailID,
JobNumber = o.Order.JobNumber,
OrderDate = o.Order.OrderDate,
OrderID = o.Order.OrderID,
OrderRef = o.Order.OrderID,
Payment = o.Order.Paid,
Reprinting = (bool?)(!o.Order.OrderPrintings.Any() || o.Order.OrderPrintings.Max(p => p.PrintDate) < o.Order.OrderDate) ?? false,
Type = o.Order.Door == null ? "Null Door" : o.Order.Door.FireDoor ?? false ? "Fire Door" : "Door Set"
}));
我把iQueryable分开了,试着帮我调试一下,但它没有帮助。现在,就像我上面说过的那样,我每天可能会得到4到5次错误,并且每10秒运行一次,大部分时间都很好。
仅供参考:修正,完成和重印字段是唯一的布尔值。
我想要找出的是当它应该是一个布尔值时从SQL返回为null。调试器在整个AddRange块上停止,到达所示代码的末尾。如果SQL分析器只能向我显示返回的数据,我会很高兴。
我尝试了什么:
我想在这里做的就是解决哪个演员失败了,如果我在看到错误的时候能看到返回的数据那么我会是一个快乐的兔子,但我的谷歌foo未能返回任何有用的答案如何做到这一点。检查_orders会导致结果的枚举,SQL会传回一组新的数据,这次它一切都很好。如果我按F5并继续,它继续正常。我可以直接围绕这个循环并继续尝试直到它起作用但这对我来说似乎不对。
修改 这是订单中的OrderLine类:
public class OrderLine
{
public bool Amendment { get; set; }
public int BasketID { get; set; }
public string Company { get; set; }
public bool Completed { get; set; }
public DateTime? DeliveryDate { get; set; }
public string DSNumber => $"DS{(JobNumber ?? "").PadLeft(7, '0')}";
public int? EmailID { get; set; }
public int EmailType => Type == "Prepped Slab" ? 2 : 1;
public string FriendlyDate => OrderDate.HasValue ? OrderDate.Value.FriendlyDate() : "";
public string FriendlyDeliveryDate => DeliveryDate?.ToShortDateString() ?? "";
public string JobNumber { get; set; }
public DateTime? OrderDate { get; set; }
public int OrderID { get; set; }
public string OrderIdentifier => $"{Type},{OrderID},{BasketID}";
[Display(Name = "View")]
[Link(Target = "doorvault")]
public string OrderPrintURL => $"/Order/ViewEmail/{BasketID}?type={EmailType}&orderid={OrderID}";
public int OrderRef { get; set; }
public string Payment { get; set; }
public int Position { get; set; }
public bool Reprinting { get; set; }
public string Type { get; set; }
public bool ViewEmail { get; set; }
}