我想你想要的东西,如:
from forum in Forums
// ForumID part removed from both sides: LINQ should do that for you.
// Added "into postsInForum" to get a group join
join post in Posts on forum equals post.Forum into postsInForum
select new
{
Forum = forum,
// Select the number of shown posts within the forum
PostCount = postsInForum.Where(post => post.ShowIt == 1).Count()
}
或(在评论中指出),你可以把一个条件在Count
电话 - 我总是忘记的可用:)
from forum in Forums
// ForumID part removed from both sides: LINQ should do that for you.
// Added "into postsInForum" to get a group join
join post in Posts on forum equals post.Forum into postsInForum
select new
{
Forum = forum,
// Select the number of shown posts within the forum
PostCount = postsInForum.Count(post => post.ShowIt == 1)
}
“显示”为唯一的过滤另一种选择职位将要做到这一点的加入:
from forum in Forums
join post in Posts.Where(post => post.ShowIt == 1)
on forum equals post.Forum into shownPostsInForum
select new
{
Forum = forum,
// Select the number of shown posts within the forum
PostCount = shownPostsInForum.Count()
}
我相信,所有的这些都逻辑正确的,但我不知道SQL将是什么样子?
任何人都知道,如果我可以在SQL服务器上做些什么来优化这些查询?我有同样的情况,但需要多个Count()结果 – 2010-05-04 22:44:19