【问题标题】:join, group by and count sql to linq conversion加入、分组和计数 sql 到 linq 的转换
【发布时间】:2017-08-26 18:42:42
【问题描述】:

查询:

SELECT COUNT(*) as Avalied
FROM LeaveMaster as lm
JOIN LeaveSubsidary as ls 
ON lm.TransMasterId = ls.TransMasterId
WHERE lm.SystemCode = 'abc123' AND lm.EmployeeCode = '0014' AND lm.Status ='Approved'
Group by ls.Leave_Type

如何将上述 sql 查询转换为 linq?谢谢:)

编辑:

from p in db.PY_LeaveTransactionMasterTAB.AsEnumerable()
join e in db.PY_LeaveTransactionSubsidaryTAB.AsEnumerable() on p.LV_TransMasterId equals e.LV_TransMasterId into grp
where p.SystemCode == SysCode && p.EmployeeCode == EmpCode && p.Status == "Approved"
group grp by e.Leave_Type // This line is invalid Why?

以上是我到目前为止尝试过的,为什么我不能e.Leave_Type或者group grp by e.Leave_Type的正确方法是什么?

【问题讨论】:

标签: sql linq linq-to-sql asp.net-mvc-5


【解决方案1】:

你可以试试这个:

(from p in db.PY_LeaveTransactionMasterTAB
join e in db.PY_LeaveTransactionSubsidaryTAB on p.LV_TransMasterId equals e.LV_TransMasterId 
where p.SystemCode == SysCode && p.EmployeeCode == EmpCode && p.Status == "Approved"
group new {p,e} by new {e.Leave_Type } into gr
select new 
{
   Leave_Type = gr.Key.Leave_Type ,
   Count = gr.Count()
}).ToList();

【讨论】:

  • 这就是答案!
猜你喜欢
  • 2013-07-19
  • 2012-05-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-07
  • 1970-01-01
相关资源
最近更新 更多