asp.net ef5 left join 的实现

时间:2022-09-18 08:55:08

最近在网上查找了很多的ef5实现left join的方法,很遗憾没有找到满意的结果,经过自己的研究终于发现了点眉目,所以分享一下



EF5: var x = ef.SYS_MODULE.GroupJoin(ef.SYS_ROLEMODULES, m => m.MKID, n => n.MKID, (w, c) => new { A = c, v = w });


说明:上面是用表SYS_MODULE left join 表 SYS_ROLEMODULES 用表SYS_MODULE.MKID 和 SYS_ROLEMODULES.MKID 关联

查询的结果放到标红的位置;但不知道为什么会自动产生C1,C2


x.ToString()的结果是:

SELECT "Project1"."MKID"  AS "MKID",
       "Project1"."C1"    AS "C1",
       "Project1"."MKMC"  AS "MKMC",
       "Project1"."C2"    AS "C2",
       "Project1"."JSID"  AS "JSID",
       "Project1"."MKID1" AS "MKID1"
  FROM (SELECT "Extent1"."MKID" AS "MKID",
               "Extent1"."MKMC" AS "MKMC",
               1 AS "C1",
               "Extent2"."JSID" AS "JSID",
               "Extent2"."MKID" AS "MKID1",
               CASE
                 WHEN ("Extent2"."JSID" IS NULL) THEN
                  NULL
                 ELSE
                  1
               END AS "C2"
          FROM "A1"."SYS_MODULE" "Extent1"
          LEFT OUTER JOIN "A1"."SYS_ROLEMODULES" "Extent2"
            ON ("Extent1"."MKID" = "Extent2"."MKID")
           AND ('1' = "Extent2"."MKID")) "Project1"
 ORDER BY "Project1"."MKID" ASC, "Project1"."C2" ASC


x.ToList()的结果是标红位置的集合;