首页 > 数据库技术 > 详细

Linq to Sql 动态条件另类实现方法

时间:2014-03-08 07:18:19      阅读:601      评论:0      收藏:0      [点我收藏+]

其实我也不知道是不是另类的,反正我找了好久园子里和其他资源。

无外乎两类

1,构造动态表达式的,这个真心繁琐,我是懒人,不想弄表达式。

2,拼SQL语句,直接执行,这个和ado.net就没有啥区别了。

我想继续用Linq,有不想用上面的两种方法,于是我测试了下面这种方法,结果完全符合预期,看看是怎么写的吧。

记录在这里,以备查阅

bubuko.com,布布扣
                var result = from s in ct.dbContext.LT_Survey
                             join r in ct.dbContext.LT_Inquiry on s.InquiryCode equals r.InquiryCode
                             join ig in ct.dbContext.LM_InquiryGuide on r.GuideNo equals ig.GuideCode into temp
                             from tt in temp.DefaultIfEmpty()
                             where
                              ((!string.IsNullOrEmpty(notificationDateS) && r.NotificationDate.CompareTo(notificationDateS) >= 0) || string.IsNullOrEmpty(notificationDateS))
                             && ((!string.IsNullOrEmpty(notificationdateE) && r.NotificationDate.CompareTo(notificationdateE) <= 0) || string.IsNullOrEmpty(notificationdateE))
                             && (s.ForEarthwork == 0 || s.ForEarthwork == 1)
                             select new
                             {
                                 s.ForEarthwork,
                                 r.GuideNo,
                                 r.NoticeDate
                                 
                             };
bubuko.com,布布扣

主要看where后面的两句,notificationDateS,和notificationDateE是一个开始日期和结束日期,在画面上是动态条件。可以不输入,或是输入其中一个,或是两个都输入。实现的要点就是这一句(!string.IsNullOrEmpty(notificationDateS) && r.NotificationDate.CompareTo(notificationDateS) >= 0) || string.IsNullOrEmpty(notificationDateS),我只能说linq很智能,他能推测出,如果notificationDateS为空,这一窜就是个恒等于真的表达式,linq会忽略掉这个条件。

看看画面不同的输入,生成的sql大家就知道了

notificationDateS:2014/03/03

notificationDateE:空白

bubuko.com,布布扣
SELECT [t0].[ForEarthwork], [t1].[GuideNo], [t1].[NoticeDate]
FROM [dbo].[LT_Survey] AS [t0]
INNER JOIN [dbo].[LT_Inquiry] AS [t1] ON [t0].[InquiryCode] = ([t1].[InquiryCode])
LEFT OUTER JOIN [dbo].[LM_InquiryGuide] AS [t2] ON [t1].[GuideNo] = [t2].[GuideCode]
WHERE ([t1].[NotificationDate] >= @p0) AND (([t0].[ForEarthwork] = @p1) OR ([t0].[ForEarthwork] = @p2))
bubuko.com,布布扣

notificationDateS:2014/03/03

notificationDateE:2014/03/13

bubuko.com,布布扣
SELECT [t0].[ForEarthwork], [t1].[GuideNo], [t1].[NoticeDate]
FROM [dbo].[LT_Survey] AS [t0]
INNER JOIN [dbo].[LT_Inquiry] AS [t1] ON [t0].[InquiryCode] = ([t1].[InquiryCode])
LEFT OUTER JOIN [dbo].[LM_InquiryGuide] AS [t2] ON [t1].[GuideNo] = [t2].[GuideCode]
WHERE ([t1].[NotificationDate] >= @p0) AND ([t1].[NotificationDate] <= @p1) AND (([t0].[ForEarthwork] = @p2) OR ([t0].[ForEarthwork] = @p3))
bubuko.com,布布扣

notificationDateS:空白

notificationDateE:空白

bubuko.com,布布扣
SELECT [t0].[ForEarthwork], [t1].[GuideNo], [t1].[NoticeDate]
FROM [dbo].[LT_Survey] AS [t0]
INNER JOIN [dbo].[LT_Inquiry] AS [t1] ON [t0].[InquiryCode] = ([t1].[InquiryCode])
LEFT OUTER JOIN [dbo].[LM_InquiryGuide] AS [t2] ON [t1].[GuideNo] = [t2].[GuideCode]
WHERE ([t0].[ForEarthwork] = @p0) OR ([t0].[ForEarthwork] = @p1)
bubuko.com,布布扣

完全符合我的要求哦,good!这种方法以前有人用过吗?不知道各位还有没有更好的动态sql方法哈,欢迎讨论哦。

Linq to Sql 动态条件另类实现方法,布布扣,bubuko.com

Linq to Sql 动态条件另类实现方法

原文:http://www.cnblogs.com/xiashengwang/p/3586366.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!