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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T23:19:39+00:00 2026-05-15T23:19:39+00:00

i am investigating into the way LINQ gets mapped to SQL, and I have

  • 0

i am investigating into the way LINQ gets mapped to SQL, and I have some troubles getting a simple thing.

There is a table sessions that holds one row per user login. Consider the following SQL


SELECT COUNT(*) AS c
FROM sessions
GROUP BY sessions.user_id
ORDER BY c DESC

I am using LINQPad to test LINQ-to-SQL transformations.

The strait-forward


    from s in Sessions group s by s.User_id into logins 
    orderby logins.Count() descending
    select new { c = logins.Count() }

gives me


SELECT [t1].[value2] AS [c]
FROM (
    SELECT COUNT(*) AS [value], COUNT(*) AS [value2]
    FROM [sessions] AS [t0]
    GROUP BY [t0].[user_id]
    ) AS [t1]
ORDER BY [t1].[value] DESC

The modified


    (from s in Sessions group s by s.User_id into logins 
    select new { c = logins.Count() }).OrderByDescending(v => v.c)

gets mapped to


SELECT [t1].[value2] AS [c]
FROM (
    SELECT COUNT(*) AS [value], COUNT(*) AS [value2]
    FROM [sessions] AS [t0]
    GROUP BY [t0].[user_id]
    ) AS [t1]
ORDER BY [t1].[value] DESC

I just can’t get this optimal output. Is there a way?
Or, maybe, I shouldn’t be concerned as the SQL will get optimized anyway?

  • 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-15T23:19:40+00:00Added an answer on May 15, 2026 at 11:19 pm

    Or, maybe, I shouldn’t be concerned as the SQL will get optimized anyway?

    Compare the execution plans of the “optimal” sql and the generated sql. (Sql Studio – click on “display estimated execution plan”)


    By inspection and drawing upon my years of clicking that button – the generated subquery presents no concern at all.

    Even this query should be fine (meaning: costs no additional io)

    SELECT [t1].[value2] AS [c] 
    FROM ( 
        SELECT COUNT(*) AS [value], COUNT(*) AS [value2] 
        FROM [sessions] AS [t0] 
        GROUP BY [t0].[user_id] 
        ) AS [t1] 
    ORDER BY [t1].[value] DESC 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been investigating Table-Valued Parameters in SQL Server 2008, and I've discovered that
Recently, I was investigating into some details about classes in Ruby, and was confused
I have a MS SQL Srver 2005 Database, that has a table with a
I've been investigating frame breaking code recently and have come across some really bizarre
I am investigating a way to use bamboo to distribute complex environment changes to
I am investigating since a few hours the best way to use the Email
Previously I have been investigating several solutions how to register with email address instead
I have been investigating about creating a UDP server, at first i though i
I'm currently investigating a way to develop a website that will allow me to
I am looking into using the new SQL Server Express LocalDB (I think it

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.