我有两张桌子如下:
1)员工
2)项目
员工表记录:
empid empname project_id
1 abc 101
2 pqr 100
3 lmn 99
4 abc 99
5 abc 100
项目表:
pid pname
99 jknkj
100 nkj
101 kjkjn
现在我想获取正在处理3个项目的员工的员工姓名?
我想要linq query.can任何人和我一起使用linq查询???
答案 0 :(得分:3)
如果您想获取正在处理3个项目的员工,请执行以下操作:
var Counts =
from e in employees
group p by e.empname into g
where g.Count == 3
select new { Employee = g.Key, ProjectCount = g.Count() };
如果你想要获取正好有3名员工工作的获取项目,那么就这样做:
var Counts =
from e in employees
group p by e.projectid into g
where g.Count == 3
select new { Porject= g.Key, EmployeeCount = g.Count() };
答案 1 :(得分:1)
您可以使用GroupBy
并根据Employees
对project_id
进行分组,然后获取包含3
名员工的群组:
db.Employees.GroupBy(x => x.project_id)
.Where(x => x.Count() == 3)
.SelectMany(x => x.Select(e => e.empname));
修改:以下评论是正确的,您可以按emp_name
对记录进行分组,以便解决问题:
db.Employees.GroupBy(x => x.empname)
.Where(x => x.Count() == 3)
.Select(x => x.Key);