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


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

    无外乎两类

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

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

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

    记录在这里,以备查阅

                    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
                                     
                                 };

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

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

    notificationDateS:2014/03/03

    notificationDateE:空白

    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))

    notificationDateS:2014/03/03

    notificationDateE:2014/03/13

    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))

    notificationDateS:空白

    notificationDateE:空白

    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)

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

  • 相关阅读:
    codeforces 985 F. Isomorphic Strings
    Educational Codeforces Round 44
    codeforces 979D
    ARC060 Digit Sum II
    Iroha and Haiku II
    Unhappy Hacking II
    Just h-index 2018湘潭邀请赛
    [HAOI2007]理想的正方形
    P1231 教辅的组成
    最小割数学形式
  • 原文地址:https://www.cnblogs.com/xiashengwang/p/3586366.html
Copyright © 2020-2023  润新知