有如下表结构
我需要按记录没有工作流文件夹的状态计算转录数。这可以解决问题:
from p in Transcriptions
where p.WorkflowfolderID == null
group p by p.TranscriptionStatus.Description into grouped
select new
{
xp=grouped.Key,
xp1= grouped.Count(),
}
现在我需要添加Dueon日期过去的记录数,因为它已经超过了截止日期。类似于
EntityFunctions.DiffHours(p.DueOn,DateTime.Today)>0
如何在不触发 2 个 SQL 查询的情况下将其包含在结果集中?我很高兴将它作为每一行中具有相同值的第三列。无论如何也可以将百分比纳入混合中,如下所示:
状态 | 计数 | % |
------------------
状态1 | 20 | 20%
状态2 | 30 | 30%
状态3 | 30 | 30%
逾期 |20 | 20%
我已将过期添加为一行,但很高兴将其作为具有相同值的列。
编辑内容
好吧,这是我能想到的最好的。它不是一个查询,但只有一个 SQL 行程。结果是:
状态 | 计数
----------------
状态1 | 20
状态2 | 30
状态3 | 30
逾期 |20
var q1= from p in Transcriptions
where p.WorkflowfolderID == null
group p by p.TranscriptionStatus.Description into grouped
select new
{
status= (string)grouped.Key,
count= grouped.Count()
};
var q2 =(
from p in Transcriptions select new {status = "Overdue",
count = (from x in Transcriptions
where x.DueOn.Value < DateTime.Now.AddHours(-24)
group x by x.TranscriptionID into
grouped select 1).Count() }).Distinct();
q1.Union(q2)
它是一个 Union 子句,一旦返回结果,就会进行 % 计算。奇怪的是,我想不出任何干净的方法来表示 LINQ 语句中的以下 SQL,这导致 var q2 中的 LINQ 相当混乱。
SELECT COUNT(*) , 'test' FROM [Transcription]