考慮下面的PL/SQL查詢:在LINQ中表示Oracle LISTAGG()功能?
SELECT department_id, LISTAGG(last_name, '; ')
WITHIN GROUP (ORDER BY last_name) "Emp_list"
FROM employees
GROUP BY department_id;
將是LINQ相當於上面的? 謝謝!
考慮下面的PL/SQL查詢:在LINQ中表示Oracle LISTAGG()功能?
SELECT department_id, LISTAGG(last_name, '; ')
WITHIN GROUP (ORDER BY last_name) "Emp_list"
FROM employees
GROUP BY department_id;
將是LINQ相當於上面的? 謝謝!
編輯:既然你想要其他列,並希望分組,你不能使用我原來建議的方法。這裏有一個新的解決方案,但生成的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))
}
注意,此方法結合了查詢類和lambda語法。
這應該可以做到。我們選擇所有的姓氏,然後將其作爲最終結果加入單個字符串中。
string.Join("; ", (from employee in employees
where employee.department_id == 30
orderby employee.last_name
select employee.last_name))
謝謝!對不起,我應該提供一個更詳細的示例,不僅返回組合字符串,還有其他一些列。我更新了我的問題中的SQL語句。 – uncoder
@uncoder我用一個新的解決方案更新了我的答案 – Connor
謝謝,但這似乎並不奏效:''System.NotSupportedException:LINQ to Entities無法識別該方法'System.String Join(System.String,System.Collections .Generic.IEnumerable'1 [System.String])'方法,並且此方法不能轉換爲商店表達式。 at System.Data.Objects.ELinq.ExpressionConverter.MethodCallTranslator.DefaultTranslator.Translate(ExpressionConverter parent,MethodCallExpression call)...''' – uncoder
爲什麼不使用'string.Join'([Example](http://www.dotnetperls.com/string-join))? –
是的,但是您還希望返回表中的其他列嗎?請參閱更新SQL。 – uncoder