LINQ 查询在 LINQPad 中有效,但在 C# 中有效

LINQ query works in LINQPad but in C#

LINQ 以下查询在 LINQPad 中按预期工作,但是当我在 Web API 中使用它时,它 returns Nullable object must have a value 异常。

这是我的代码。

        var GRStk = (from g in GRStkB4
              join t1 in opTOs on g.itemCode equals t1.itemCode into temp1
              from op in temp1.DefaultIfEmpty()
              select new
              {
                   g.branchCode,
                   g.itemCode,
                   qty = g.availQty - op.opqty
               }).AsEnumerable().Where(w => w.qty > 0); // exception

       int ii = GRStk.Count(); // Exception with Nullable object must have a value.

我用Asp.Net Core 2.2 我无法理解它在 LINQPad 中的工作原理以及此处可以为空的内容是什么?

使用 EF 时,查询中的某些命令在客户端进行评估,但在 linqPad 中不进行评估。也就是说,如果 op 为空,那么如果在客户端对其进行评估,您可能无法访问 op.opqty。

我想更改以下行将解决您的问题:

...
                        select new
              {
                   g.branchCode,
                   g.itemCode,
                   qty = g.availQty - (op!= null ?  op.opqty : 0) //edit here!
               }).AsEnumerable().Where(w => w.qty > 0); // exception exception
...