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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T19:44:48+00:00 2026-05-11T19:44:48+00:00

I have a LINQ to SQL query: from at in Context.Transaction select new {

  • 0

I have a LINQ to SQL query:

from at in Context.Transaction
select new  {
    at.Amount,
    at.PostingDate,
    Details = 
        from tb in at.TransactionDetail
        select new {
            Amount = tb.Amount,
            Description = tb.Desc
        }
}

This results in one SQL statement being executed. All is good.

However, if I attempt to return known types from this query, even if they have the same structure as the anonymous types, I get one SQL statement executed for the top level and then an additional SQL statement for each “child” set.

Is there any way to get LINQ to SQL to issue one SQL statement and use known types?

EDIT: I must have another issue. When I plugged a very simplistic (but still hieararchical) version of my query into LINQPad and used freshly created known types with just 2 or 3 members, I did get one SQL statement. I will post and update when I know more.

EDIT 2: This appears to be due to a bug in Take. See my answer below for details.

  • 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-11T19:44:48+00:00Added an answer on May 11, 2026 at 7:44 pm

    First – some reasoning for the Take bug.

    If you just Take, the query translator just uses top. Top10 will not give the right answer if cardinality is broken by joining in a child collection. So the query translator doesn’t join in the child collection (instead it requeries for the children).

    If you Skip and Take, then the query translator kicks in with some RowNumber logic over the parent rows… these rownumbers let it take 10 parents, even if that’s really 50 records due to each parent having 5 children.

    If you Skip(0) and Take, Skip is removed as a non-operation by the translator – it’s just like you never said Skip.

    This is going to be a hard conceptual leap to from where you are (calling Skip and Take) to a “simple workaround”. What we need to do – is force the translation to occur at a point where the translator can’t remove Skip(0) as a non-operation. We need to call Skip, and supply the skipped number at a later point.

    DataClasses1DataContext myDC = new DataClasses1DataContext();
      //setting up log so we can see what's going on
    myDC.Log = Console.Out;
    
      //hierarchical query - not important
    var query = myDC.Options.Select(option => new{
      ID = option.ParentID,
      Others = myDC.Options.Select(option2 => new{
        ID = option2.ParentID
      })
    });
      //request translation of the query!  Important!
    var compQuery = System.Data.Linq.CompiledQuery
      .Compile<DataClasses1DataContext, int, int, System.Collections.IEnumerable>
      ( (dc, skip, take) => query.Skip(skip).Take(take) );
    
      //now run the query and specify that 0 rows are to be skipped.
    compQuery.Invoke(myDC, 0, 10);
    

    This produces the following query:

    SELECT [t1].[ParentID], [t2].[ParentID] AS [ParentID2], (
        SELECT COUNT(*)
        FROM [dbo].[Option] AS [t3]
        ) AS [value]
    FROM (
        SELECT ROW_NUMBER() OVER (ORDER BY [t0].[ID]) AS [ROW_NUMBER], [t0].[ParentID]
        FROM [dbo].[Option] AS [t0]
        ) AS [t1]
    LEFT OUTER JOIN [dbo].[Option] AS [t2] ON 1=1 
    WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p1 + @p2
    ORDER BY [t1].[ROW_NUMBER], [t2].[ID]
    -- @p0: Input Int (Size = 0; Prec = 0; Scale = 0) [0]
    -- @p1: Input Int (Size = 0; Prec = 0; Scale = 0) [0]
    -- @p2: Input Int (Size = 0; Prec = 0; Scale = 0) [10]
    -- Context: SqlProvider(Sql2005) Model: AttributedMetaModel Build: 3.5.30729.1
    

    And here’s where we win!

    WHERE [t1].[ROW_NUMBER] BETWEEN @p0 + 1 AND @p1 + @p2
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 154k
  • Answers 154k
  • 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 96 sounds pretty accurate to me. I think you're confusing… May 12, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer (\d+\.\d+|\d+) should do the trick. May 12, 2026 at 10:27 am
  • Editorial Team
    Editorial Team added an answer I don't see how. The PID doesn't make it onto… May 12, 2026 at 10:27 am

Related Questions

Strange performance outcome, I have a LINQ to SQL query which uses several let
I have to perform the following SQL query: select answer_nbr, count(distinct user_nbr) from tpoll_answer
i get this error {Method 'System.DateTime ConvertTimeFromUtc(System.DateTime, System.TimeZoneInfo)' has no supported translation to SQL.}
I've just started working with ASP.NET MVC now that it's in beta. In my

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.