[英]LINQ OUTER JOIN Query - Null in Where clause not working
In my following sample data tables the customer C1 has ordered Vegetables and C2 has ordered Fruits. 在我的以下样本数据表中,客户C1订购了蔬菜,C2订购了水果。 I want to display name of potential customers C3 and C4 who have not orders yet.
我想显示尚未订购的潜在客户C3和C4的名称。 So, I use an
Outer Join
but query always returns the first cutomer C1. 所以,我使用
Outer Join
但查询总是返回第一个cutomer C1。 It seems, something wrong with my where clause. 看来,我的where子句有问题。
Customers table : 客户表 :
CustomerID CustName
1 C1
2 C2
3 C3
4 C4
Orders table : 订单表 :
OrderID CustomerID OrderType
1 1 V
2 2 F
3 1 V
LINQ Query to display potential customers with no orders yet: 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());
}
}
UPDATE : 更新 :
According to @IvanStoev, this seems to be an EF core bug. 根据@IvanStoev的说法,这似乎是一个EF核心错误。 I should have pointed out that I'm using the following technologies: ASP.NET CORE, EF Core, Visual Studio 2015 - UPDATE 3, and SQL Server Express 2014. I'm including a tag for EF Core in the post.
我应该指出我正在使用以下技术:ASP.NET CORE,EF Core,Visual Studio 2015 - UPDATE 3和SQL Server Express 2014.我在帖子中包含了EF Core的标签。
If someone can find a solution or a workaround, please let me know. 如果有人可以找到解决方案或解决方法,请告诉我。
UPDATE 2 : 更新2 :
SQL Server Profiler captures the following following SQL: 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''
But my view is showing only one record. 但我的观点只显示一条记录。 Also, when I place a breakpoint at
@for (int t=0; t<Model.Count; t++)
in my view shown below, it shows only 1 at the Model.Count
: 另外,当我在下面显示的视图
@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>
This is actually your query and works (against say Northwind): 这实际上是你的查询和工作(反对说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;
But simplier way to say that is: 但更简单的说法是:
var Qry = from c in Customers
where !c.Orders.Any()
select c;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.