如何执行select count(*) from UnrelatedTable where documentLink_Id = x.documentLink_id
并将结果计数添加到totalTimeAccessed
。两个表格(PublishedContent
和UnrelatedTable
都有documentLink_id
)。然后我想只选择计数大于0的行
var dbData = publishedontext.PublishedContent
.Where(x => x.Status > PublishedStatus.Released)
.OrderBy(x => x.Title)
.Select(x => new DownloadsPerContetnItemReportRows()
{
title = x.Title,
documentLink_Id = x.DocumentLink_Id,
statustatus = x.Status,
totalTimeAccessed = "Select count from an unrelated table"
}
答案 0 :(得分:1)
以下代码应有效:
totalTimeAccessed = PublishedContent
.Join(UnrelatedTable,pc => pc.DocumentLink_Id,
urt => urt.DocumentLink_Id,(pc,urt) => new {pc,urt})
.Where(y => pc.Count > 0 && urt.Count > 0)
工作原理
PublishedContent
加入UnrelatedTable
Count > 0
和PublishedContent
投影的UnrelatedTable
是否根据您的具体要求,您只需要进行一些修改。如果Count
引用的行数不是投影列,那么Join
无论如何会给出匹配的行,那么我们就不需要Where
子句