在LINQ中表达Oracle LISTAGG()功能?

时间:2016-04-22 23:25:17

标签: c# sql linq oracle11gr2 listagg

考虑以下PL / SQL查询:

SELECT department_id, LISTAGG(last_name, '; ')
     WITHIN GROUP (ORDER BY last_name) "Emp_list"
FROM employees
GROUP BY department_id;

LINQ会与上述相当吗? 谢谢!

1 个答案:

答案 0 :(得分:0)

编辑:由于您需要其他列并希望分组,因此您无法使用我最初提出的方法。这是一个新的解决方案,虽然生成的SQL并不是很好:

from employee in employees
group employee by employee.department_id into grpEmployee
select new {
    DepartmentId = grpEmployee.Key,
    LastNames = string.Join(", ", employees.Where(e => e.department_id == grpEmployee.Key)
                                  .OrderBy(e => e.last_name).Select(e => e.last_name))
}

请注意,此方法结合了类似query和lambda的语法。

这应该可以解决问题。我们选择所有姓氏,然后将它们连接成一个字符串作为最终结果。

string.Join("; ", (from employee in employees
                   where employee.department_id == 30
                   orderby employee.last_name
                   select employee.last_name))