sql查询vs linq到实体返回不同的结果

时间:2013-02-19 23:20:33

标签: sql linq

这是我的SQL查询

SELECT TOP(10)projects.stat, 
              wo.stat, 
              sevt.restype, 
              sevt.resid, 
              restype.user2, 
              projects.prj_id, 
              projects.user3, 
              projects.user9, 
              wo.wonum, 
              wo.jobdesc, 
              sevt.sesid, 
              sevt.restype, 
              sevt.type, 
              sevt.t_start, 
              sevt.t_end, 
              sevt. mealstart, 
              sevt.mealend, 
              sevt. melstart2, 
              sevt.melend2, 
              sevt.melstart3, 
              sevt.melend3, 
              sevt.user2, 
              sevt.subactid, 
              sevt.ot_exempt, 
              sevt_ex.user5, 
              rescat.user1 
FROM   schedwin.projects 
       INNER JOIN schedwin.wo 
               ON projects.prj_id = wo.prj_id 
       INNER JOIN schedwin.sevt 
               ON wo.seqnum = sevt.seqnum 
       INNER JOIN schedwin.rsrce 
               ON sevt.resid = rsrce.resid 
       LEFT OUTER JOIN schedwin.pers 
                    ON rsrce.recid = pers.recid 
       INNER JOIN schedwin.restype 
               ON sevt.rtype = restype.code 
       INNER JOIN schedwin.rescat 
               ON sevt.rcat = rescat.code 
       LEFT OUTER JOIN schedwin.sevt_ex 
                    ON sevt.sesid = sevt_ex.sesid 
WHERE  ( Ltrim(Rtrim(projects.stat)) IN ( '1', '2' ) ) 
       AND ( Ltrim(Rtrim(wo.stat)) = '6' ) 
       AND ( ( ( sevt.restype = 5 
                  OR sevt.restype = 0 ) 
               AND ( Substring(restype.user2, 2, 1) = 'F' 
                     AND Substring(restype.user2, 6, 1) = 'S' ) ) 
              OR ( sevt.restype = 4 ) ) 
       AND ( sevt.type = 0 ) 
       AND rescat.groupid = 0 
       AND restype.groupid = 0 
       AND Len(Ltrim(wo.invoice)) > 0 
       AND Ltrim(wo.invoice) <> 'PENDING' 
       AND wo.userflag1 <> 1 
       AND Ltrim(sevt.t_start) = '1351728000' 
ORDER  BY projects.prj_id, 
          wo.wonum 

我正在将Linq转换为实体,如下所示

            var query =
            (from PROJECTS in db.PROJECTS
            join WOes in db.WOes on PROJECTS.PRJ_ID equals WOes.PRJ_ID
            join SEVTs in db.SEVTs on WOes.SEQNUM equals SEVTs.SEQNUM
            join RSRCEs in db.RSRCEs on SEVTs.RESID equals RSRCEs.RESID
            join PERS in db.PERS on RSRCEs.RECID equals PERS.RECID into PERS_join
            from PERS in PERS_join.DefaultIfEmpty()
            join RESTYPEs in db.RESTYPEs on new { RTYPE = SEVTs.RTYPE } equals new { RTYPE = RESTYPEs.CODE }
            join RESCATs in db.RESCATs on new { RCAT = SEVTs.RCAT } equals new { RCAT = RESCATs.CODE }
            join SEVT_EX in db.SEVT_EX on SEVTs.SESID equals SEVT_EX.SESID into SEVT_EX_join
            //join SEVT_EX in db.SEVT_EX on new { SESID = (String)SEVTs.SESID } equals new { SESID = SEVT_EX.SESID } into SEVT_EX_join
            from SEVT_EX in SEVT_EX_join.DefaultIfEmpty()
            where
              (new string[] { "1", "2" }).Contains((PROJECTS.STAT.TrimEnd()).TrimStart()) &&
              ((WOes.STAT.TrimEnd()).TrimStart() == "6") &&
              (((SEVTs.RESTYPE == 5 || SEVTs.RESTYPE == 0) &&
              (RESTYPEs.USER2.Substring(2 - 1, 1) == "F" && RESTYPEs.USER2.Substring(6 - 1, 1) == "S")) || (SEVTs.RESTYPE == 0)) &&
              (SEVTs.TYPE == 0) &&
              (RESCATs.GROUPID == 0) &&
              (RESTYPEs.GROUPID == 0 )&&
              (int?)(WOes.INVOICE.TrimStart()).Length > 0 &&
              WOes.INVOICE.TrimStart() != "PENDING" &&
              WOes.USERFLAG1 != 1 &&
             (SEVTs.T_START.TrimStart()) == (Booktime)
             //(SEVTs.T_START.TrimStart()) == (Booktime)
             // String.Compare(SEVTs.T_START.ToString().TrimStart(), "1351728000") >= 0
            //Convert.ToInt32(SEVTs.T_START.TrimStart()) >= Convert.ToInt32(Booktime)
            orderby
             PROJECTS.PRJ_ID,
             WOes.WONUM
            select new
            {
                PROJECTS.PRJ_ID,
                PROJECTS.USER3,
                PROJECTS.USER9,
                WOes.WONUM,
                WOes.JOBDESC,
                SEVTs.SESID,
                SEVTs.RESTYPE,
                SEVTs.TYPE,
                SEVTs.T_START,
                SEVTs.T_END,
                SEVTs.MEALEND,
                SEVTs.MELSTART3,
                SEVTs.MELSTART2,
                SEVTs.MELEND2,
                Column1 = SEVTs.MELSTART2,
                SEVTs.MELEND3,
                SEVTs.USER2,
                SEVTs.SUBACTID,
                SEVTs.OT_EXEMPT,
                USER5 = SEVT_EX.USER5,
                SEVTs.GMT_OFFSET,
                SEVTs.MEALSTART,
                SEVTs.STANDARD,
                RESCATs.USER1,
                SEVTs.RESID
            }).Take(10); 

肯定我的sql查询返回正确的记录但是我的Linq不是,任何有任何线索请问这里缺少什么?

由于

1 个答案:

答案 0 :(得分:1)

你有这一行:

(((SEVT.restype = 5 or SEVT.restype = 0) and (substring(restype.User2,2,1) = 'F' and substring(restype.User2,6,1) = 'S')) or (SEVT.RESTYPE = 4))and 

在linq你有这个

((SEVTs.RESTYPE == 5 || SEVTs.RESTYPE == 0) && (RESTYPEs.USER2.Substring(2 - 1, 1) == "F" && RESTYPEs.USER2.Substring(6 - 1, 1) == "S") && SEVTs.TYPE == 0)

首先,2-1中的子串= 1而不是2.逻辑运算不等于