5

我有一张员工桌子和EmployeeCourseStatus桌子。

我想显示每个员工的列表以及完成的课程数(status = "CMP")。

我有以下相关子查询,导致以下错误:

var query = (from emp in Employee
                     join adr in EmployeeAddress on emp.id = adr.EmployeeID
                     select new
                     {
                        id = emp.id,
                        name=emp.name,
                        country=adr.country,
                        CompletedCourseCount = (from c in employeeCourseStatus where c.empid = emp.id && c.status == "CMP" select c.id).count()
                     }

错误:

仅支持 Premitive 类型。

等效的 SQL 子查询将是 -

Select emp.id
       , emp.name
       , adr.Country
       , CompletedCourseCount = (select count(id) from EmployeeCourseStatus where id = emp.id and status = "CMP") 
from   Employee emp
       JOIN employeeaddress adr ON adr.EmployeeID = emp.ID
4

3 回答 3

3

equals加入序列时使用关键字

var query = from emp in Employee
            join adr in EmployeeAddress on emp.id equals adr.EmployeeID
            join c in EmployeeCourseStatus on emp.id equals c.empid into courses
            select new
            {
               id = emp.id,
               name = emp.name,
               country = adr.country,
               CompletedCourseCount = courses.Where(x => x.status == "CMP").Count()
            };
于 2012-11-16T23:59:41.727 回答
1

我更喜欢使用 lambda 表达式(为了可读性 - 特别是在 Join 方法中):

Employee.Join(EmployeeAddress, emp => emp.id, adr => adr.EmployeeID, (emp, adr) => new
{
    id = emp.id,
    name = emp.name,
    country = adr.country,
    CompletedCourseCount = employeeCourseStatus.Count(c => c.empid == emp.id && c.status == "CMP")
});
于 2012-11-17T00:02:50.967 回答
0

请尝试在您的计数查询中替换where c.empid = emp.id为。where c.empid == emp.id

如果这不起作用,emp.name和的类型是adr.country什么?

于 2012-11-16T23:59:24.717 回答