0

我正在尝试确定当前用户是否对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();

如何修复此查询?

4

2 回答 2

2

SelectSingleOrDefault替换为Any

var hasVoted = Model.Project.ProjectDoc
                    .Where(a => a.Current == true && a.DocType == "Cover")
                    .Any(v => v.ProjectDocVote
                               .Any(u => u.UserID == ViewBag.CurrentUserID));
于 2013-02-06T16:48:33.797 回答
1
var count = Model.Project.ProjectDoc
   .Where(a => a.Current == true 
            && a.DocType == "Cover"
            && a.ProjectDocVote.Any(v => v.UserID == ViewBag.CurrentUserID))
   .Count():
于 2013-02-06T16:51:36.210 回答