我知道有很多类似的问题,我已经解决了很多问题,但是仍然无法掌握如何在LINQ中解决问题。我需要创建一个查询来获取数据,作为此纯SQL查询:
SELECT p.ProcessId, p.Description, p.StartedOn, p.StartedBy, uuus.Name + ' ' + uuus.Surname AS StartedByName, p.FinishedOn, p.FinishedBy, uuu.Name + ' ' + uuu.Surname as FinishedByName, p.PlannedFinish, p.PlannedStart, COUNT(CASE WHEN h.IsCompleted IS NULL AND h.HandlingId IS NOT NULL THEN 1 END)
FROM JDE_Processes p LEFT JOIN JDE_Users uuu ON p.FinishedBy = uuu.UserId LEFT JOIN JDE_Handlings h ON h.ProcessId=p.ProcessId LEFT JOIN JDE_Users uuus ON uuus.UserId=p.StartedBy
GROUP BY p.ProcessId, p.Description, p.StartedOn, p.StartedBy,uuus.Name + ' ' + uuus.Surname, p.FinishedOn, p.FinishedBy, uuu.Name + ' ' + uuu.Surname, p.PlannedFinish, p.PlannedStart, p.Createdon
ORDER BY p.CreatedOn DESC
这是我的linq版本,大多数情况下都可以运行,但是我无法弄清楚“如果h.IsCompleted = 0计数”部分。
var items = (from p in db.JDE_Processes
join uuu in db.JDE_Users on p.FinishedBy equals uuu.UserId into finished
from fin in finished.DefaultIfEmpty()
join h in db.JDE_Handlings on p.ProcessId equals h.ProcessId into hans
from ha in hans.DefaultIfEmpty()
group new { p, fin }
by new {
p.ProcessId,
p.Description,
p.StartedOn,
p.StartedBy,
p.FinishedOn,
p.FinishedBy,
p.PlannedFinish,
p.PlannedStart,
fin.Name,
fin.Surname
} into grp
orderby grp.Key.ProcessId descending
select new Process
{
ProcessId = grp.Key.ProcessId,
Description = grp.Key.Description,
StartedOn = grp.Key.StartedOn,
StartedBy = grp.Key.StartedBy,
FinishedOn = grp.Key.FinishedOn,
FinishedBy = grp.Key.FinishedBy,
FinishedByName = grp.Key.Name + " " + grp.Key.Surname,
PlannedStart = grp.Key.PlannedStart,
PlannedFinish = grp.Key.PlannedFinish,
HandlingStatus = grp.Count().ToString()
});
问题:
1)如何获取“如果h.IsCompleted = 0起作用”的计数?
2)我可以使用某种别名吗?我的意思是,在此查询的实际版本中,还有另一个左联接join uuu in db.JDE_Users on p.StartedBy equals uuu.UserId into started
from star in started.DefaultIfEmpty()
这导致我在grp中有2个Name和2个Surname列。然后如何将适当的字段分配给适当的输出字段?我的意思是这样:
select new Process
{
ProcessId = grp.Key.ProcessId,
Description = grp.Key.Description,
StartedOn = grp.Key.StartedOn,
StartedBy = grp.Key.StartedBy,
StartedByName = grp.Key.Name + " " + grp.Key.Surname, // <-- how will it know which Name field to use?
FinishedOn = grp.Key.FinishedOn,
FinishedBy = grp.Key.FinishedBy,
FinishedByName = grp.Key.Name + " " + grp.Key.Surname,
PlannedStart = grp.Key.PlannedStart,
PlannedFinish = grp.Key.PlannedFinish,
HandlingStatus = grp.Count().ToString()
}
答案 0 :(得分:1)
我不确定这将有多大帮助,因为它是您的SQL的翻译,并且您的LINQ似乎与您的SQL没有关系,但是我有:
var ans = from p in db.JDE_Processes
join uuu in db.JDE_Users on p.FinishedBy equals uuu.UserId into uuuj
from uuu in uuuj.DefaultIfEmpty()
join h in db.JDE_Handlings on p.ProcessId equals h.ProcessId into hj
from h in hj
group new { p, h } by new { p.ProcessId, p.Description, p.StartedOn, p.StartedBy, p.FinishedOn, p.FinishedBy, p.PlannedFinish, p.PlannedStart } into phg
select new {
phg.Key.ProcessId,
phg.Key.Description,
phg.Key.StartedOn,
phg.Key.StartedBy,
phg.Key.FinishedOn,
phg.Key.FinishedBy,
phg.Key.PlannedFinish,
phg.Key.PlannedStart,
HandlingStatus = phg.Where(ph => ph.h.IsCompleted == null).Count()
};
答案 1 :(得分:0)
很抱歉回答我自己的问题,但我已经找到2的答案。当我有两个同名的属性(例如“姓”)时,我只是不知道如何为属性创建别名。请在下面找到包含别名以及NetMage解决的条件计数器部分的代码:
Response