LINQ to SQL null Query
More than once in the Forum, some netizens asked questions about the query of LINQ null. Here we take the Microsoft northwind database as an example to summarize:
For example, how can I implement an SQL statement such as query using LINQ?
SELECT *FROM [Orders] AS [t0]WHERE ([t0].[ShippedDate]) IS NULL
VMethod 1:
from o in Orderswhere o.ShippedDate==nullselect o
The corresponding Lamda expression is:
Orders .Where (o => (o.ShippedDate == (DateTime?)null))
The corresponding SQL statement is:
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]FROM [Orders] AS [t0]WHERE [t0].[ShippedDate] IS NULL
VMethod 2:
from o in Orderswhere Nullable<DateTime>.Equals(o.ShippedDate,null)select o
The corresponding Lamda expression is:
Orders .Where (o => Object.Equals (o.ShippedDate, null))
The corresponding SQL statement is:
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]FROM [Orders] AS [t0]WHERE [t0].[ShippedDate] IS NULL
VMethod 3:
from o in Orderswhere !o.ShippedDate.HasValueselect o
The corresponding Lamda expression is:
Orders .Where (o => !(o.ShippedDate.HasValue))
The corresponding SQL statement is:
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]FROM [Orders] AS [t0]WHERE NOT ([t0].[ShippedDate] IS NOT NULL)
VMethod 4:
from o in Orderswhere o.ShippedDate.Value==(DateTime?)nullselect o
The corresponding Lamda expression is:
Orders .Where (o => ((DateTime?)(o.ShippedDate.Value) == (DateTime?)null))
The corresponding SQL statement is:
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]FROM [Orders] AS [t0]WHERE ([t0].[ShippedDate]) IS NULL
VMethod 5:
from o in Orderswhere System.Data.Linq.SqlClient.SqlMethods.Equals(o.ShippedDate.Value,null)select o
The corresponding Lamda expression is:
Orders .Where (o => Object.Equals (o.ShippedDate.Value, null))
The corresponding SQL statement is:
SELECT [t0].[OrderID], [t0].[CustomerID], [t0].[EmployeeID], [t0].[OrderDate], [t0].[RequiredDate], [t0].[ShippedDate], [t0].[ShipVia], [t0].[Freight], [t0].[ShipName], [t0].[ShipAddress], [t0].[ShipCity], [t0].[ShipRegion], [t0].[ShipPostalCode], [t0].[ShipCountry]FROM [Orders] AS [t0]WHERE ([t0].[ShippedDate]) IS NULL
The above methods are only implemented in the LINQ to SQL validation, and the LINQ to EF is not verified.