Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 762021
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:54:28+00:00 2026-05-14T15:54:28+00:00

I have 2 tables, Forums and Posts. I want to retrieve all Forums fields

  • 0

I have 2 tables, Forums and Posts.
I want to retrieve all Forums fields with a new extra field: count all post that belong to this forum.

I have this for now:

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()
  1. The join must be Left join: if there are no post that belongs to some forum, the forums fields should be retrieved but PostCount field should be 0.
  2. The result set must be distinct (join gives me the full cross…or how it’s called)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:54:29+00:00Added an answer on May 14, 2026 at 3:54 pm

    I think you want something like:

    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()
    }
    

    Or (as pointed out in the comments) you can put a condition in the Count call – I always forget that’s available 🙂

    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)
    }
    

    Another alternative for the filtering of only “shown” posts would be to do it in the join:

    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()
    }
    

    I believe all of these are logically correct, but I don’t know what the SQL will look like…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two tables - forum_topics and topics_posts . I want to select rows
I have a database named bbs, which have 37 tables. I want to find
I have a forum-like site, which allows users to post. I want users to
I want to preface this question by stating that I am fairly new to
I have 3 tables: forums id, name, description threads thread_id, forum_id, user_id, title, content,
Image that I have a huge database which stores threads and posts from different
I have a table of forum posts, and want to improve the basic search
I have created a feature where users can start new topics (similar to forums).
I have a postgresql table storing posts from an online forum. Each post belongs
I am creating a What's New section that lists all of the database changes

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.