LINQ TO SQL Null 查詢

來源:互聯網
上載者:User
LINQ TO SQL   Null 查詢

 

在論壇裡不止一次看到有網友提問關於LINQ NULL查詢的問題了,現以微軟NorthWind 資料庫為例總結一下:

如查詢這樣一句SQL ,用LINQ如何??

SELECT *FROM [Orders] AS [t0]WHERE ([t0].[ShippedDate]) IS NULL

 

方法一:

from o in Orderswhere o.ShippedDate==nullselect o

對應的Lamda運算式為:

Orders   .Where (o => (o.ShippedDate == (DateTime?)null))

對應的SQL語句為:

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

 

方法二:

from o in Orderswhere Nullable<DateTime>.Equals(o.ShippedDate,null)select o

對應的Lamda運算式為:

Orders   .Where (o => Object.Equals (o.ShippedDate, null))

對應的SQL語句為:

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

 

方法三:

from o in Orderswhere !o.ShippedDate.HasValueselect o

對應的Lamda運算式為:

Orders   .Where (o => !(o.ShippedDate.HasValue))

對應的SQL語句為:

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)

 

方法四:

from o in Orderswhere o.ShippedDate.Value==(DateTime?)nullselect o

對應的Lamda運算式為:

Orders   .Where (o => ((DateTime?)(o.ShippedDate.Value) == (DateTime?)null))

 對應的SQL語句為:

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

 

方法五:

from o in Orderswhere  System.Data.Linq.SqlClient.SqlMethods.Equals(o.ShippedDate.Value,null)select o

對應的Lamda運算式為:

Orders    .Where (o => Object.Equals (o.ShippedDate.Value, null))

對應的SQL語句為:

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

以上方法均只在LINQ TO SQL內驗證實現,LINQ TO EF未驗證。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.