LINQ TO SQL Null 查询 .

LINQ TO SQL   Null 查询

 http://blog.csdn.net/q107770540/article/details/7348384

在论坛里不止一次看到有网友提问关于LINQ NULL查询的问题了,现以微软NorthWind 数据库为例总结一下:

如查询这样一句SQL ,用LINQ如何实现?

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

 

方法一:

  1. from o in Orders  
  2. where o.ShippedDate==null  
  3. select o  

对应的Lamda表达式为:

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

对应的SQL语句为:

  1. 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]  
  2. FROM [Orders] AS [t0]  
  3. WHERE [t0].[ShippedDate] IS NULL  

 

方法二:

  1. from o in Orders  
  2. where Nullable<DateTime>.Equals(o.ShippedDate,null)  
  3. select o  

对应的Lamda表达式为:

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

对应的SQL语句为:

  1. 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]  
  2. FROM [Orders] AS [t0]  
  3. WHERE [t0].[ShippedDate] IS NULL  

 

方法三:

  1. from o in Orders  
  2. where !o.ShippedDate.HasValue  
  3. select o  

对应的Lamda表达式为:

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

对应的SQL语句为:

  1. 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]  
  2. FROM [Orders] AS [t0]  
  3. WHERE NOT ([t0].[ShippedDate] IS NOT NULL)  

 

方法四:

  1. from o in Orders  
  2. where o.ShippedDate.Value==(DateTime?)null  
  3. select o  

对应的Lamda表达式为:

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

 对应的SQL语句为:

  1. 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]  
  2. FROM [Orders] AS [t0]  
  3. WHERE ([t0].[ShippedDate]) IS NULL  

 

方法五:

  1. from o in Orders  
  2. where  System.Data.Linq.SqlClient.SqlMethods.Equals(o.ShippedDate.Value,null)  
  3. select o  

对应的Lamda表达式为:

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

对应的SQL语句为:

  1. 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]  
  2. FROM [Orders] AS [t0]  
  3. WHERE ([t0].[ShippedDate]) IS NULL  


以上方法均只在LINQ TO SQL内验证实现,LINQ TO EF未验证。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值