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 8097087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T21:38:57+00:00 2026-06-05T21:38:57+00:00

I got two tables: comments and commentLikes in the same query i count the

  • 0

I got two tables: comments and commentLikes
in the same query i count the likes users have given on a comment.

I got the following (simplified) query:

var res = (from c in db.Comments
                           where c.Topic.ID == topicID
                           select new
                           {
                               comment = c,
                               count = c.CommentLikes.Count()
                           }).ToList();

But, rather than mapping the likecount into the comment entity again, I’d like to get a list of Comments only with a field LikeCount in it, preferably with an efficient query. Something like this:

   var res = (from c in db.Comments
                               where c.Topic.ID == topicID
                               select new
                               {
                                   comment = c,
                                   c.LikeCount = c.CommentLikes.Count()
                               }).ToList();

This query doesn’t compile.

How to do this in linq?

  • 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-06-05T21:38:59+00:00Added an answer on June 5, 2026 at 9:38 pm

    You can’t do that. EF does not support to project (= select) data into an entity. You must fill the LikeCount property in memory after the query has been executed. You can write it in a compact way, but it’s basically just a foreach loop over the materialized anonymous objects:

    IEnumerable<Comment> res =
              (from c in db.Comments
               where c.Topic.ID == topicID
               select new
               {
                   comment = c,
                   count = c.CommentLikes.Count()
               })
               .ToList() // DB query runs here, the rest in memory
               .Select(a => {
                   a.comment.LikeCount = a.count;
                   return a.comment;
               });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i've got two tables: members and members_data . They both share the same unique
I have got two tables: Event +----------+---------+--------------+ | event_id | name | date |
I have got two tables in SQL Server 2005: USER Table: information about user
I got two tables: users and firms: class Firm extends AppModel { var $name
I got two tables with the same pk ( idnumber ).. I want to
I've got two tables that have related items through a connection-table. The connection table
I've got two tables - users and servers , and for the HABTM relationship,
Got two tables: users(id, column) and objects(user_id, column) . How to extract all users
I've got two tables (articles and tags) that have a one-to-many relationship. I remember
I have two tables: invoice and charges, with a one-to-many relationship. (simplified) invoice has:

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.