[英]LINQ OUTER JOIN Query - Null in Where clause not working
在我的以下样本数据表中,客户C1订购了蔬菜,C2订购了水果。 我想显示尚未订购的潜在客户C3和C4的名称。 所以,我使用Outer Join
但查询总是返回第一个cutomer C1。 看来,我的where子句有问题。
客户表 :
CustomerID CustName
1 C1
2 C2
3 C3
4 C4
订单表 :
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核心错误。 我应该指出我正在使用以下技术:ASP.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++)
断点放在@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>
这实际上是你的查询和工作(反对说Northwind):
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;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.