有人可以将此查询翻译成Linq吗?此查询计算员工的出勤人数。
select
count(attandance.empid),
employee2.empname
from
employee2
inner join
attandance
on employee2.empid=attandance.empid
group by
employee2.empname
答案 0 :(得分:1)
这应该会得到与sql相同的结果。如果您使用linq到sql或EF,只需用数据源替换两个List。
List<Employee2> employee2 = new List<Employee2>
{
new Employee2 { EmpId = 1, EmpName = "Bob" },
new Employee2 { EmpId = 2, EmpName = "Sam" },
new Employee2 { EmpId = 3, EmpName = "Jim" },
};
List<Attendance> attandance = new List<Attendance>
{
new Attendance { EmpId = 1 },
new Attendance { EmpId = 2 },
new Attendance { EmpId = 2 },
new Attendance { EmpId = 3 },
new Attendance { EmpId = 3 },
new Attendance { EmpId = 3 },
};
var r = from emp in employee2
join att in attandance on emp.EmpId equals att.EmpId
group emp by emp.EmpName into g
select new { EmpName = g.Key, Count = g.Count() };
答案 1 :(得分:0)
这应该有效:
var attendance = (from e in db.employee2 // db would be your datacontext
select new
{
empName = e.empname,
attendanceCount = (from a in db.attandance
where a.empid == e.empid).Count()
});
如果您不想使用datacontext,可以通过连接数据源在linqpad中尝试上述linq。