LINQ OUTER JOIN查询中Where子句无效



在我下面的示例数据表中,客户C1订购了蔬菜,C2订购了水果。我想显示还没有订单的潜在客户C3和C4的名字。因此,我使用Outer Join,但查询总是返回第一个客户C1。我的where子句好像有问题。

<<p> 客户表/strong>:
CustomerID  CustName
1           C1
2           C2
3           C3
4           C4
<<p> 订单表/strong>:
OrderID CustomerID  OrderType
1       1           V
2       2           F
3       1           V

LINQ查询显示没有订单的潜在客户:

    public class TestDbController : Controller
    {
       public async Task<IActionResult> TestAction(List<CustomersViewModel> list)
       {
          var Qry = from c in Customers
                     join ord in Orders on c.CustomerId equals ord.CustomerId into c_o
                     from t in c_o.DefaultIfEmpty()
                     where t == null
                     select new  CustomersViewModel() {CustName = c.Name};
          return View(qry.ToList());
        }
    }

:

根据@IvanStoev,这似乎是一个EF核心错误。我应该指出,我正在使用以下技术:. NET CORE, EF CORE, Visual Studio 2015 - UPDATE 3和SQL Server Express 2014。我在文章中加入了EF Core的标签。

如果有人能找到解决方案或变通方法,请告诉我。

更新2 :

SQL Server Profiler捕获以下SQL:

exec sp_executesql N'SELECT [c].[CustNumber], @__Empty_0
FROM [Customers] AS [c]
LEFT JOIN [Orders] AS [ord] ON [c].[CustomerID] = [ord].[CustomerID]
ORDER BY [c].[CustomerID]',N'@__Empty_0 nvarchar(4000)',@__Empty_0=N''

但是我的视图只显示一条记录。另外,当我在下面的视图中在@for (int t=0; t<Model.Count; t++)处放置一个断点时,它在Model.Count处只显示1:

@model List<MyProject.Models.MyViewModels.CustomersViewModel>
    <form asp-controller="TestDb" asp-action="TestAction" method="post">
    <table class="table">
      <thead>
         <tr>
             <th><label asp-for="@Model.First().CustName"></label></th>
         </tr>
       </thead>
       <tbody>
             @for (int t=0; t<Model.Count; t++)
                {
                  <tr>
                     <td><input type="hidden" asp-for="@Model[t].CustomerId"/ </td>
                     <td><input type="text" asp-for="@Model[t].CustName"/></td>
                  </tr>
                }
       </tbody>
      </table>
      <button type="submit">Save</button>
  </form>

这实际上是你的查询和工作(对说北风):

var Qry = from c in Customers
              join ord in Orders on c.CustomerID equals ord.CustomerID into c_o
              from t in c_o.DefaultIfEmpty()
              where t == null
              select c;

但更简单的说法是:

var Qry = from c in Customers
          where !c.Orders.Any()
          select c;

最新更新