我正在尝试确定当前用户是否已投票ProjectDoc
。此查询的值为1. ProjectDocVote
表中没有与UserID
匹配的值。 ViewBag.CurrentUserID
值是正确的。
var count = Model.Project.ProjectDoc
.Where(a => a.Current == true && a.DocType == "Cover")
.Select(v => v.ProjectDocVote
.SingleOrDefault(u => u.UserID == ViewBag.CurrentUserID)
)
.Count();
如何修复此查询?
答案 0 :(得分:2)
将Select
和SingleOrDefault
替换为Any
:
var hasVoted = Model.Project.ProjectDoc
.Where(a => a.Current == true && a.DocType == "Cover")
.Any(v => v.ProjectDocVote
.Any(u => u.UserID == ViewBag.CurrentUserID));
答案 1 :(得分:1)
var count = Model.Project.ProjectDoc
.Where(a => a.Current == true
&& a.DocType == "Cover"
&& a.ProjectDocVote.Any(v => v.UserID == ViewBag.CurrentUserID))
.Count():