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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:58:53+00:00 2026-05-13T01:58:53+00:00

I’ve the following tables: Paciente -> PacienteTag -> Tag The typical N:M between Paciente

  • 0

I’ve the following tables:

Paciente -> PacienteTag -> Tag

The typical N:M between Paciente and Tag with an intermediary table.

I want to obtain how many patients have each tag. Simple:

SELECT Tag.TagName, COUNT(PacienteTag.TagId) AS Totals  FROM Tag
LEFT JOIN PacienteTag ON Tag.TagId = PacienteTag.TagId
GROUP BY Tag.TagName
ORDER BY Tag.TagName

The result is a simple table like this:

Tag1, 0
Tag2, 0
Tag3, 0
Tag4, 2
Tag5, 0
Etc…

However when I created my LINQ2SQL(given my limited LINQ2SQL knowledge) version of that, the results came like:

Tag1, 1
Tag2, 1
Tag3, 1
Tag4, 2
Tag5, 1
Etc…

Puzzled, but more or less understanding what LINQ was doing, I downloaded Linqer and told it to translate the SQL Expression for me.

It came up with this:

from t in db.Tag
join t0 in db.PacienteTag on t.TagId equals t0.TagId into t0_join
from t0 in t0_join.DefaultIfEmpty()
group t by new {
  t.TagName
} into g
orderby
  g.Key.TagName
select new {
  g.Key.TagName,
  Totals = (Int64?)g.Count()
}

Which is not far from my approach. But surprisingly, that also comes back with the same wrong results!

I can see where the error “is” but I cannot come with an alternative.

WHat am I doing wrong?

NOTE: in the above example, the table PacienteTag has two records that associate Tag4 with two random patients, my T-SQL is returning the right values.

Ideas?

  • 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-13T01:58:53+00:00Added an answer on May 13, 2026 at 1:58 am

    Assuming that your relationships are set up correctly and there is a reference to PacienteTags from Tag called PacienteTags, the following should work.

    from t in db.Tag orderby t.TagName 
    select new { t.TagName, Totals = g.PacienteTags.Count() };
    

    I’ve found that the need to join in LINQ to (anything) is pretty rare… just utilize the generated references to get at data in other tables (or entities if using EF).

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

Sidebar

Related Questions

I have the following tables in Oracle 11g: Table A SITE_ID SITE_NAME VECTOR .........MANY
Suppose that we have following tables create table Employee( 2 EMPNO NUMBER(3), 3 ENAME
I have the following tables. I want to run a query but I think
I have the following tables: Persons, Person_Categories and Persons_PersonCategories which is a linking table
I have the following tables: CREATE TABLE IF NOT EXISTS `Person_Categories` ( `PrsCatID` int(11)
I have the following tables: --table sportactivity-- sport_activity_id, home_team_fk, away_team_fk, competition_id_fk, date, time (tuple
I have the following tables Client Table and Product Table ID Name ClientProduct Table
I have the following tables: FACULTY table CREATE TABLE FACULTY ( FACULTY_ID NUMBER(3,0), FACULTY_NAME
I have the following tables: Table A: id int v1 string v2 string Table
I have the following tables - groups, contacts, contacts_groups (habtm join table) groups &

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.