全雨秋*_*全雨秋 5 c# linq asp.net-core-2.1 ef-core-2.1
SELECT
e.EmpName,
me.RemarkNumber,
me.RemarkPeopleNumber
FROM
EmployeeInfo e
LEFT JOIN (
SELECT
COUNT(RemarkId) As RemarkNumber,
COUNT(DISTINCT MemberId) As RemarkPeopleNumber,
CreateUser
FROM
MemberRemark
WHERE
RemarkStatus = 0
GROUP BY
CreateUser
) AS me On e.EmpName = me.CreateUser
WHERE
BranchCode = '0000'
And [Status] = 0
Run Code Online (Sandbox Code Playgroud)
如何将其转换为 LINQ?
from e in db.EmployeeInfo
join me in (
from memberRemarks in db.MemberRemark
where memberRemarks.RemarkStatus == 0
group memberRemarks by new
{
memberRemarks.CreateUser,
} into g
select new
{
RemarkNumber = g.Count(),
RemarkPeopleNumber = g.Select(m => m.MemberId).Distinct().Count(),
g.Key.CreateUser
}
) on new { e.EmpName } equals new { EmpName = me.CreateUser } into meJoin
from me in meJoin.DefaultIfEmpty()
where e.BranchCode == "0000" & &e.Status == 0
select new
{
e.EmpName,
me.RemarkNumber,
me.RemarkPeopleNumber
};
Run Code Online (Sandbox Code Playgroud)
我收到这个错误
RemarkPeopleNumber = g.Select(m=>m.MemberId).Distinct().Count(), //错误
使用 asp.net core mvc 2.1 + ef core 2.1 + mssql
也许如果你把查询分成几部分,处理起来会更好?使用我的SQL to LINQ Recipe我会像这样翻译你的 SQL:
var ePart = from e in db.EmployeeInfo
where e.BranchCode == "0000" && e.Status == 0
select e;
var mrPart = from mr in db.MemberRemark
where mr.RemarkStatus == 0
group mr by mr.CreateUser into mrg
select new {
CreateUser = mrg.Key,
RemarkNumber = mrg.Count(),
RemarkPeopleNumber = mrg.Select(mr => mr.MemberId).Distinct().Count()
};
var ans = from e in ePart
join me in mrPart on e.EmpName equals me.CreateUser into mej
from me in mej
select new {
e.EmpName,
me.RemarkNumber,
me.RemarkPeopleNumber
};
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1656 次 |
| 最近记录: |