此SQL查询的linq-2-sql语法是什么:
SELECT emp.id, Name, Count(t.id) as CNT
FROM employee emp
LEFT JOIN taskAssignment t
on emp.id = t.FKEmployeeID GROUP BY emp.id, Name
表是这样的:
答案 0 :(得分:2)
以下是答案
var lst = from emp in Employeetables
join task in TaskAssignmentTables
on emp.EmployeeId equals task.FKEmployeeId into j
from result in j.DefaultIfEmpty()
group result by new { emp.EmployeeId, emp.Name } into groupResult
select new
{
EmployeeId = groupResult.Key.EmployeeId,
Name = groupResult.Key.Name,
Count = groupResult.Count(r => r.FKEmployeeId != null)
};
这将返回与此SQL Left outer join question相关的SQL问题相同的答案。我只是使用LinqPad
开发了这个答案 1 :(得分:0)
不确定这是否有效,但绝对值得一试。
如果它没有按预期工作,请告诉我它在数据库上触发了什么查询,以便我可以相应地进行改进。
List<Employee> employee = new List<Employee>()
{
new Employee() { id = 1, Name = "Samar" },
new Employee() { id = 1, Name = "Samar" },
new Employee() { id = 1, Name = "Samar" },
new Employee() { id = 2, Name = "Sid" }
};
List<TaskAssignment> taskAssignment = new List<TaskAssignment>()
{
new TaskAssignment(){FKEmployeeID = 1},
new TaskAssignment(){FKEmployeeID = 1}
};
var cls = from e in employee
join emp in taskAssignment on e.id equals emp.FKEmployeeID into empout
group e by new { e.id, e.Name } into g
select new { g.Key.id, g.Key.Name, CNT = g.Count() };
希望这有帮助。
答案 2 :(得分:0)
试试这个。
var employees = from emp in dbContext.Employees
join task in dbContext.TaskAssignmentTable
on emp.employeeID equals task.FKEmployeeID
into tEmpWithTask
from tEmp in tEmpWithTask.DefaultIfEmpty()
group tEmp by new { emp.EmployeeID, emp.Name } into grp
select new {
grp.Key.EmployeeID,
grp.Key.Name,
grp.Count(t=>t.TaskID != null)
};