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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T14:00:53+00:00 2026-06-16T14:00:53+00:00

I’m running into a problem with the following pseudoquery: var daily = from p

  • 0

I’m running into a problem with the following pseudoquery:

var daily = from p in db.table1
            group p by new
            {
                key1,
                key2
            } into g
            join d in db.table2
            on new { p.key1, p.key2 } equals { d.key1, d.key2 }
            select new
            {
                col1 = g.Key.key1
                col2 = g.Sum(a => a.column2)
                col3 = d.column3
            };

It runs but the generated SQL statement that LINQ sends to SQL Server is absurd. The actual implementation follows a similar setup as above with 7 or so more columns that each have a .Sum() calculation. The generated SQL has somewhere around 10-11 nested SELECT statements with no INNER JOIN and, of course, takes forever to run.

I tested out another implementation of the query:

var daily = from p in
                (from p in db.table1
                 group p by new
                 {
                     key1,
                     key2
                 } into g
                 select new
                 {
                     col1 = g.Key.key1,
                     col2 = g.Sum(a => a.column2)
                 })
            join d in db.table2
            on new { p.key1, p.key2 } equals new { d.key1, d.key2 }
            select new
            {
                col1 = p.col1,
                col2 = p.col2,
                col3 = d.column3
            };

This version generates far more reasonable SQL with a single SUB-SELECT and an INNER JOIN statement (it also runs damn near instantly). The thing I hate about this is that the first LINQ query is, IMHO, far more straight-forward and concise whereas the second seems rather redundant since I end up having to define all the columns I want from table1 twice.

Why do these two similar queries perform so much differently on the server and why does query 2 end up being far more efficient even though it’s code is far less expressive?

Is there a way I can rewrite the first query to be as efficient as the second?

  • 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-16T14:00:54+00:00Added an answer on June 16, 2026 at 2:00 pm

    LINQ 2 SQL has a problem with the following pattern:

    from t in table
    group t by key into g
    from t in g //"ungroup" the grouping - this is causing a problem
    select ...
    

    I think your join is triggering that because it “ungroups” the grouping. Note that a LINQ join is a GroupJoin which is unrepresentable in SQL. Think about it: How would you translate my example query? You have to join table to a grouped version of table causing insane redundancy.

    I have seen this problem a few times. You have found the correct work-around: Force a projection to prevent this pattern from occurring.

    There is a slightly less awkward version:

    var daily = from p in db.table1
                group p by new
                {
                    key1,
                    key2
                } into g
                select new
                {
                    col1 = g.Key.key1,
                    col2 = g.Sum(a => a.column2)
                } into p
                join d in db.table2 on new { p.key1, p.key2 } equals new { d.key1, d.key2 }
                select new
                {
                    col1 = p.col1,
                    col2 = p.col2,
                    col3 = d.column3
                };
    

    The nesting is removed by the lesser known select x into y syntax.

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

Sidebar

Related Questions

I am currently running into a problem where an element is coming back from
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a French site that I want to parse, but am running into
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I want use html5's new tag to play a wav file (currently only supported
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this

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.