shi*_*esh 10 c# linq entity-framework
我有2张桌子,论坛和帖子.
我想要使用新的额外字段检索所有论坛字段:计算属于此论坛的所有帖子.
我现在有这个:
var v =(from forum in Forums
join post in Posts on forum.ForumID equals post.Forum.ForumID
select new
{
forum, //Need to retrieve all fields/columns from forum
PostCount = //count all post that belong to this forum with a condition: count it only if post.Showit==1
}
).Distinct()
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 22
我想你想要的东西:
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()
}
Run Code Online (Sandbox Code Playgroud)
或者(如评论中所指出的)你可以在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)
}
Run Code Online (Sandbox Code Playgroud)
仅过滤"显示"帖子的另一种方法是在连接中执行此操作:
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()
}
Run Code Online (Sandbox Code Playgroud)
我相信所有这些在逻辑上都是正确的,但我不知道SQL会是什么样子......
归档时间: |
|
查看次数: |
21199 次 |
最近记录: |