sql查询到linq(使用带两个键的join)

yduiuuwa  于 2021-07-26  发布在  Java
关注(0)|答案(1)|浏览(413)

我得到了sql查询并尝试使用linq,因为如果ef。有人能看一下吗?请告诉我哪里错了?sql查询正在工作,但linq不返回任何数据。基本上有两个表a和表b。我想加入a\b,这是(使我的数据存在于表a中,但不在表b中)

SqlQueryText = "  SELECT A.EmployeeID" +
               "  FROM EmployeeCourseModels A " +
               "  LEFT JOIN EmployeeTrainingModels B " +
              "   ON A.EmployeeID = B.EmployeeID and a.CourseID = B.CourseID" +
              "   WHERE(B.CourseID is null and B.EmployeeID is null) AND A.CourseID = " + courseID;

我试过了,但没用。当我连接两个表时,我需要两个键值来匹配,正如您从sql表中看到的那样。

var ReqList = from course in employeeCourseRepository.EmployeeCourseList
              join training in employeeTrainingRepository.EmployeeTrainingList
              on new { c = course.CourseID, e = course.EmployeeID } equals new { c = training.CourseID, e = training.EmployeeID }
              where  (course.CourseID == Blist.CourseID)
                           select new  {  empID = course.EmployeeID };
kmpatx3s

kmpatx3s1#

原始查询是左连接,而代码是内部查询,请将其更改为:

var ReqList = from course in employeeCourseRepository.EmployeeCourseList.Where(c => c.CourseID == Blist.CourseID)
              join training in employeeTrainingRepository.EmployeeTrainingList
              on new { c = course.CourseID, e = course.EmployeeID } equals new { c = training.CourseID, e = training.EmployeeID } into j
              from res in j.DefaultIfEmpty()
              select new  {  empID = course.EmployeeID };

相关问题