LINQ TO SQL Null 查询
在论坛里不止一次看到有网友提问关于LINQ NULL查询的问题了,现以微软NorthWind 数据库为例总结一下:
如查询这样一句SQL ,用LINQ如何实现?
- SQL code
-
SELECT*FROM[Orders]AS[t0]WHERE ([t0].[ShippedDate]) ISNULL
方法一:
- C# code
-
from o in Orders where o.ShippedDate==nullselect o
对应的Lamda表达式为:
- C# code
-
Orders.Where (o => (o.ShippedDate == (DateTime?)null))
对应的SQL语句为:
- SQL code
-
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]ISNULL
方法二:
- C# code
-
from o in Orders where Nullable<DateTime>.Equals(o.ShippedDate,null) select o
对应的Lamda表达式为:
- C# code
-
Orders.Where (o => Object.Equals (o.ShippedDate, null))
对应的SQL语句为:
- SQL code
-
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]ISNULL
方法三:
- C# code
-
from o in Orders where!o.ShippedDate.HasValue select o
对应的Lamda表达式为:
- C# code
-
Orders.Where (o =>!(o.ShippedDate.HasValue))
对应的SQL语句为:
- SQL code
-
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]WHERENOT ([t0].[ShippedDate]ISNOTNULL)
方法四:
- C# code
-
from o in Orders where o.ShippedDate.Value==(DateTime?)nullselect o
对应的Lamda表达式为:
- C# code
-
Orders.Where (o => ((DateTime?)(o.ShippedDate.Value) == (DateTime?)null))
对应的SQL语句为:
- SQL code
-
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]) ISNULL
方法五:
- C# code
-
from o in Orders where System.Data.Linq.SqlClient.SqlMethods.Equals(o.ShippedDate.Value,null) select o
对应的Lamda表达式为:
- C# code
-
Orders.Where (o => Object.Equals (o.ShippedDate.Value, null))
对应的SQL语句为:
- SQL code
-
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]) ISNULL
以上方法均只在LINQ TO SQL内验证实现,LINQ TO EF未验证。
浙公网安备 33010602011771号