我有这个查询,它使用状态为 Completed 的 Childs 填充 CompletedWords 和 CompletedRows ,问题是它也是段表的 2 次而不是 1 次:
var query = _context.Submodules.Where(t => t.Id == id)
.Select(e => new Submodules{
Id = e.Id,
Name = e.Name,
Status = e.Status,
Token = e.Token,
ModuleId = e.ModuleId,
Gender = e.Gender,
TotalRows = e.TotalRows,
TotalWords = e.TotalWords,
CompletedWords = e.Segments.Where(a => a.Status == Abr.Recorded).Sum(y=> y.Wordcount),
CompletedRows = e.Segments.Where(a => a.Status == Abr.Recorded).Count()
}).ToList();
转化为:
SELECT t.ID, t.name, t.status, t.token, t.moduleID,
t.gender, t.total_rows AS TotalRows, t.total_words AS TotalWords,
(
SELECT SUM(a.wordcount)
FROM segments AS a
WHERE (a.status = 1) AND (t.ID = a.submoduleID)
) AS CompletedWords, (
SELECT COUNT(*)
FROM segments AS a0
WHERE (a0.status = 1) AND (t.ID = a0.submoduleID)
) AS CompletedRows
FROM submodules AS t
WHERE t.ID = @__id_0
正如您看到的那样填充 CompletedWords 和 Rows ,它运行 2 个选择,其中 Status==1 ,只有一个是 Sum ,另一个是 Count() ,我如何将它们合并到 .1 select 中。
请指教
素胚勾勒不出你
相关分类