gpt4 book ai didi

c# - Linq 中的相关子查询

转载 作者:太空狗 更新时间:2023-10-29 23:24:38 25 4
gpt4 key购买 nike

我有一个员工表和 EmployeeCourseStatus 表。

我想显示每个员工的列表以及已完成类(class)的数量(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()
}

错误:

Only Premitive types are supported.

等效的 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

最佳答案

连接序列时使用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()
};

关于c# - Linq 中的相关子查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13426097/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com