需要使用linq从这两个列表/表中获取不匹配的记录

时间:2016-05-22 14:10:05

标签: c# sql-server linq tsql

使用这两个表:

employee(id, name)
payroll(name, salary)

我需要修改&将此SQL查询转换为linq:

SELECT employee.id, employee.name, payroll.salary
FROM employee left OUTER JOIN payroll
ON employee.name like payroll.name + '%'

从这两个表中获取不匹配的记录。有什么想法吗?

1 个答案:

答案 0 :(得分:0)

您可以尝试此查询:

var query = from emp in context.employee
            join res in (from emp in context.employee
                         from pay in context.payroll
                         where emp.name.StartsWith(pay.name)
                         select new
                         {
                             emp.id,
                             pay.salary
                         })
            on emp.id equals res.id into subRes
            from sub in subRes.DefaultIfEmpty()
            select new
            {
                emp.id,
                emp.name,
                sub?.salary
            };