----------- [知识分享] LINQ TO SQL Null 查询 ---------------

q107770540 2012-03-13 02:43:55
加精
在论坛里不止一次看到有网友提问关于LINQ NULL查询的问题了,现以微软NorthWind 数据库为例总结一下:
如查询这样一句SQL ,用LINQ如何实现?
SELECT *
FROM [Orders] AS [t0]
WHERE ([t0].[ShippedDate]) IS NULL


方法一:

from o in Orders
where o.ShippedDate==null
select 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 Orders
where 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 Orders
where !o.ShippedDate.HasValue
select 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 Orders
where o.ShippedDate.Value==(DateTime?)null
select 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 Orders
where 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未验证。

具体参见myblog: http://blog.csdn.net/q107770540/article/details/7348384
...全文
11354 79 打赏 收藏 转发到动态 举报
写回复
用AI写文章
79 条回复
切换为时间正序
请发表友善的回复…
发表回复
Banianer 2012-09-14
  • 打赏
  • 举报
回复
感谢技术分享帖
zvolitation 2012-08-24
  • 打赏
  • 举报
回复
感谢分享
CodeFriends 2012-07-18
  • 打赏
  • 举报
回复
学习了,发现自己很懒嘢 从不去想第二种方法
zyouping 2012-06-29
  • 打赏
  • 举报
回复
mark~ 感学楼主,现在也在学习linq与Lamda。
alextienpai 2012-04-10
  • 打赏
  • 举报
回复
虽然很小,但是非常有用!!谢谢了!!
zzc_king 2012-04-10
  • 打赏
  • 举报
回复
看到楼主,才发现自己比菜鸟还菜鸟
裸奔在上海 2012-04-09
  • 打赏
  • 举报
回复
谢谢分享
zsu747 2012-04-08
  • 打赏
  • 举报
回复
学到了一些有用的知识
Payden 2012-04-07
  • 打赏
  • 举报
回复

感谢分享
licai1210 2012-04-06
  • 打赏
  • 举报
回复
LZ V5
swlds 2012-04-06
  • 打赏
  • 举报
回复
真好,希望以后有多点这样让人学习的机会
claymore1114 2012-04-06
  • 打赏
  • 举报
回复
收藏了
q107770540 2012-04-05
  • 打赏
  • 举报
回复
[Quote=引用 61 楼 的回复:]
以前的同事,顶下。
[/Quote]
who are you?
wxhysoftsodc 2012-04-05
  • 打赏
  • 举报
回复
以前的同事,顶下。
a160_ 2012-04-05
  • 打赏
  • 举报
回复
谢谢分享
nmy124 2012-04-03
  • 打赏
  • 举报
回复
谢谢分享,学习!!!
流星陨落 2012-04-03
  • 打赏
  • 举报
回复
好东东呀,谢谢分享,学习!!!
xiaocongzhi 2012-04-02
  • 打赏
  • 举报
回复
好东东呀
猴头 2012-04-01
  • 打赏
  • 举报
回复
惭愧,就会第一种。。。。。
猴头 2012-04-01
  • 打赏
  • 举报
回复
谢谢大哥
加载更多回复(36)

8,497

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 LINQ
社区管理员
  • LINQ
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧