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

  • Home
  • SEARCH
  • 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 6059525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:43:14+00:00 2026-05-23T08:43:14+00:00

I have a bit complex linq2sql query, it doesn’t contain any ‘order by’ statements,

  • 0

I have a bit complex linq2sql query, it doesn’t contain any ‘order by’ statements, but somehow linq2sql thinks it is necessery and inserts it. Unfortunately this ‘order by’ statement hurts performance and I don’t know how to remove it…

Here’s the linq2sql expressions (I don’t think that they might help, but anyway…)

var lockedBy = Guid.NewGuid();
var locks = LinqDataContext.Instance.LockBranches(3, lockedBy, DateTime.Now + TimeSpan.FromMinutes(3)); // Stored procedure...

var x = LinqDataContext.Instance.Branches
.Where(branch => branch.LockedBy == lockedBy)
.Select
(
    branch => new
    {
        Branch = branch,
        Leaves = branch.Leaves
        .Select
        (
            leaf => new
            {
                Leaf = leaf,
                Estimate = leaf.Representation.Estimates
                .GroupBy(estimate=>estimate.SegmentID)
                .Sum
                (
                    estimatesBySegment => estimatesBySegment.Average
                    (
                        estimate => estimate.EstimateRequests.Average
                        (
                            estimateRequest => estimateRequest.EstimateSubmit.Value
                        )
                    )
                )
            }
        )
    }
);

It renders to the following sql

SELECT [t0].[ID], [t0].[IndexID], [t0].[IndexNo], [t0].[Sealed], [t0].[LockedBy], [t0].[UnlockOn], [t1].[ID] AS [ID2], [t1].[BranchID], [t1].[RepresentationID], [t1].[Xml], (
    SELECT SUM([t7].[value])
    FROM (
        SELECT AVG([t6].[value]) AS [value]
        FROM (
            SELECT (
                SELECT AVG([t5].[Value])
                FROM [dbo].[EstimateRequests] AS [t4]
                LEFT OUTER JOIN [dbo].[EstimateSubmits] AS [t5] ON [t5].[EstimateRequestID] = [t4].[ID]
                WHERE [t4].[EstimateID] = [t3].[ID]
                ) AS [value], [t2].[ID], [t3].[RepresentationID], [t3].[SegmentID]
            FROM [dbo].[Representations] AS [t2], [dbo].[Estimates] AS [t3]
            ) AS [t6]
        WHERE ([t6].[ID] = [t1].[RepresentationID]) AND ([t6].[RepresentationID] = [t6].[ID])
        GROUP BY [t6].[SegmentID]
        ) AS [t7]
    ) AS [Estimate], (
    SELECT COUNT(*)
    FROM [dbo].[Leaves] AS [t8]
    WHERE [t8].[BranchID] = [t0].[ID]
    ) AS [value]
FROM [dbo].[Branches] AS [t0]
LEFT OUTER JOIN [dbo].[Leaves] AS [t1] ON [t1].[BranchID] = [t0].[ID]
WHERE [t0].[LockedBy] = @p0
ORDER BY [t0].[ID], [t1].[ID]  <-- Here's the unnecessary ORDER BY
  • 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-23T08:43:14+00:00Added an answer on May 23, 2026 at 8:43 am

    The order by is needed to be able to distinguish the child collection Leaves. (Every time you have a child collection there is an order by)

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

Sidebar

Related Questions

I have a little bit complex MySQL query for me and i can't figure
I have a question for which I suspect the answer is a bit complex.
I'm currently using os.popen() but have been recommended to use subprocess.popen() instead. Any ideas
I have a bit of a complex situation. I am building an iPhone app
I have a bit of a complex situation with WPF Treeview Binding. I have
My problem is a little bit complex. I have the following code: $(document).ready(function() {
I have a bit complex to explain issue that I have ran into: I
I have a bit more complex system... I have users model - hasMany Ads
I have a few modular and to some extend a little bit complex applications
This question is simple, but the set-up's a bit complex. Please bear with me...

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.