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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T07:10:24+00:00 2026-05-11T07:10:24+00:00

I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer

  • 0

I have to perform the following SQL query:

select answer_nbr, count(distinct user_nbr) from tpoll_answer where poll_nbr = 16 group by answer_nbr 

The LINQ to SQL query

from a in tpoll_answer  where a.poll_nbr = 16 select a.answer_nbr, a.user_nbr distinct  

maps to the following SQL query:

select distinct answer_nbr, distinct user_nbr from tpoll_answer where poll_nbr = 16 

So far, so good. However the problem raises when trying to GROUP the results, as I’m not being able to find a LINQ to SQL query that maps to the first query I wrote here (thank you LINQPad for making this process a lot easier). The following is the only one that I’ve found that gives me the desired result:

from answer in tpoll_answer where answer.poll_nbr = 16 _ group by a_id = answer.answer_nbr into votes = count(answer.user_nbr) 

Which in turns produces the follwing ugly and non-optimized at all SQL query:

SELECT [t1].[answer_nbr] AS [a_id], (     SELECT COUNT(*)     FROM (         SELECT CONVERT(Bit,[t2].[user_nbr]) AS [value], [t2].[answer_nbr], [t2].[poll_nbr]         FROM [TPOLL_ANSWER] AS [t2]         ) AS [t3]     WHERE ([t3].[value] = 1) AND ([t1].[answer_nbr] = [t3].[answer_nbr]) AND ([t3].[poll_nbr] = @p0)     ) AS [votes] FROM (     SELECT [t0].[answer_nbr]     FROM [TPOLL_ANSWER] AS [t0]     WHERE [t0].[poll_nbr] = @p0     GROUP BY [t0].[answer_nbr]     ) AS [t1] -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [16] -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1 

Any help will be more than appreciated.

  • 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. 2026-05-11T07:10:24+00:00Added an answer on May 11, 2026 at 7:10 am

    There isn’t direct support for COUNT(DISTINCT {x})), but you can simulate it from an IGrouping<,> (i.e. what group by returns); I’m afraid I only ‘do’ C#, so you’ll have to translate to VB…

     select new  {      Foo= grp.Key,      Bar= grp.Select(x => x.SomeField).Distinct().Count()  }; 

    Here’s a Northwind example:

        using(var ctx = new DataClasses1DataContext())     {         ctx.Log = Console.Out; // log TSQL to console         var qry = from cust in ctx.Customers                   where cust.CustomerID != ''                   group cust by cust.Country                   into grp                   select new                   {                       Country = grp.Key,                       Count = grp.Select(x => x.City).Distinct().Count()                   };          foreach(var row in qry.OrderBy(x=>x.Country))         {             Console.WriteLine('{0}: {1}', row.Country, row.Count);         }     } 

    The TSQL isn’t quite what we’d like, but it does the job:

    SELECT [t1].[Country], (     SELECT COUNT(*)     FROM (         SELECT DISTINCT [t2].[City]         FROM [dbo].[Customers] AS [t2]         WHERE ((([t1].[Country] IS NULL) AND ([t2].[Country] IS NULL)) OR (([t1] .[Country] IS NOT NULL) AND ([t2].[Country] IS NOT NULL) AND ([t1].[Country] = [ t2].[Country]))) AND ([t2].[CustomerID] <> @p0)         ) AS [t3]     ) AS [Count] FROM (     SELECT [t0].[Country]     FROM [dbo].[Customers] AS [t0]     WHERE [t0].[CustomerID] <> @p0     GROUP BY [t0].[Country]     ) AS [t1] -- @p0: Input NVarChar (Size = 0; Prec = 0; Scale = 0) [] -- Context: SqlProvider(Sql2008) Model: AttributedMetaModel Build: 3.5.30729.1 

    The results, however, are correct- verifyable by running it manually:

            const string sql = @' SELECT c.Country, COUNT(DISTINCT c.City) AS [Count] FROM Customers c WHERE c.CustomerID != '' GROUP BY c.Country ORDER BY c.Country';         var qry2 = ctx.ExecuteQuery<QueryResult>(sql);         foreach(var row in qry2)         {             Console.WriteLine('{0}: {1}', row.Country, row.Count);         } 

    With definition:

    class QueryResult {     public string Country { get; set; }     public int Count { get; set; } } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer I found it. I just expanded the regsvr32.ex_ found in… May 11, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer VARCHAR(MAX) column values will be stored IN the table row,… May 11, 2026 at 6:02 pm
  • Editorial Team
    Editorial Team added an answer Rageous is correct, there is no complex logic behind it.… May 11, 2026 at 6:02 pm

Related Questions

I am trying to perform tuning of materialized views in my application. I set
I have the following scenario: I have a database with a particular MyISAM table
I've just started learning how to use the Entity Framework to write a very
I have a website I've built in VS2005, C#, .NET 2.0. This website does

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.