好的,像这样。我需要一些信息来确定
是不是有点排队?这在 linq 中有所不同,而在 SQL 中则没有。我也不知道你的上下文名称,但你应该明白。
var query = Context.ExportQueues.Select(x => new {
QueueItem = x.QueueItem,
Sent = !x.Queued ? 1 : 0,
Queued = x.Queued ? 1 : 0,
Exported = x.Success ? 1 : 0,
Failed = !x.Success ? 1 : 0 })
.GroupBy(x => x.QueueItem)
.Select(g => new {
QueueItem = g.Key,
Sent = g.Sum(x => x.Sent),
Queued = g.Sum(x => x.Queued),
Exported = g.Sum(x => x.Exported),
Failed = g.Sum(x => x.Failed)
}).ToList();
编辑您还可以通过在查询中即时执行案例来组合这些。当我完成它时,我总是倾向于先把它写出来,尽管如果出现错误,更复杂的聚合可能有点难以调试:
var query = Context.ExportQueues
.GroupBy(x => x.QueueItem)
.Select(g => new {
QueueItem = g.Key,
Sent = g.Sum(x => !x.Queued ? 1 : 0),
Queued = g.Sum(x => x.Queued ? 1 : 0),
Exported = g.Sum(x => x.Success ? 1 : 0),
Failed = g.Sum(x => !x.Success ? 1 : 0 )
}).ToList();