LINQ to SQL null Query

Source: Internet
Author: User
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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.