我想你想要的東西,如:
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