gpt4 book ai didi

c# - EF 核心 2.1 中的 COUNT(DISTINCT *)

转载 作者:行者123 更新时间:2023-11-30 12:20:31 25 4
gpt4 key购买 nike

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

如何将其转换为 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
};

我遇到了这个错误

RemarkPeopleNumber = g.Select(m=>m.MemberId).Distinct().Count(), //error

使用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
};

关于c# - EF 核心 2.1 中的 COUNT(DISTINCT *),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51414444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com