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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T12:26:28+00:00 2026-05-20T12:26:28+00:00

I’m trying to reduce the number of database queries and in doing so I’m

  • 0

I’m trying to reduce the number of database queries and in doing so I’m attempting to retrieve a series of hierarchical entities (that were previously fetched recursively) in one go.

I’ve got a Labels property holding a set of strings:

public string[] Labels { get; set; } // new string[] {"{{a}}", "{{b}}", "{{c}}", "{{d}}", "{{e}}"};

that I’m using to construct a first query:

var IDeferredTopLabels= 
    db.labels
        .Where(l =>
            l.site_id == this.site_id &&
            this.Labels.Contains(l.name_for_code)
        )
    .Select(l => new LabelWithParentId { Label = l, ParentId = null });

The above, if inspected while debugging, would generate the following TSQL

SELECT [t0].[id], [t0].[name_for_code], [t0].[site_id], [t0].[priority_level]
FROM [dbo].[labels] AS [t0]
WHERE ([t0].[site_id] = 15) AND ([t0].[name_for_code] IN ('{{a}}', '{{b}}', '{{c}}', '{{d}}', '{{e}}')) 

So far so good, following the IDeferredToplabels declaration I’m extending the first query with a Union specifying how child Label entities should be fetched:

var IDeferredWithChildLabels = IDeferredTopLabels
    .Union(
        db.label__labels
        .Where(ll => 
            db.labels
                .Where(l =>
                    l.site_id == this.site_id &&
                    this.Labels.Contains(l.name_for_code)
                )
            .Select(l => l.id)
            .Contains(ll.parent_label_id)
        )
    .Select(ll => new LabelWithParentId { Label = ll.label1, ParentId = ll.parent_label_id})
    );

Now if I inspect the TSQL generate is:

SELECT [t4].[id], [t4].[name_for_code], [t4].[site_id], [t4].[priority_level], [t4].[value] AS [ParentId]
FROM (
    SELECT [t0].[id], [t0].[name_for_code], [t0].[site_id], [t0].[priority_level], NULL AS [value]
    FROM [dbo].[labels] AS [t0]
    WHERE ([t0].[site_id] = 15) AND ([t0].[name_for_code] IN ('{{a}}', '{{b}}', '{{c}}', '{{d}}', '{{e}}'))
    UNION
    SELECT [t2].[id], [t2].[name_for_code], [t2].[site_id], [t2].[priority_level], [t1].[parent_label_id] AS [value]
    FROM [dbo].[label__label] AS [t1]
    INNER JOIN [dbo].[labels] AS [t2] ON [t2].[id] = [t1].[label_id]
    WHERE EXISTS(
        SELECT NULL AS [EMPTY]
        FROM [dbo].[labels] AS [t3]
        WHERE ([t3].[id] = [t1].[parent_label_id]) AND ([t3].[site_id] = 15) AND ([t3].[name_for_code] IN ('{{a}}', '{{b}}', '{{c}}', '{{a}}'0, '{{a}}'1))
        )
    ) AS [t4]

If you see at the end of this second TSQL Query, the part IN ('{{a}}', '{{b}}', '{{c}}', '{{a}}'0, '{{a}}'1)), the set of strings have some unespected 0 and 1 next to the second last and last element plus it is no longer the initial set of strings (ie ‘{{a}}’, ‘{{b}}’, ‘{{c}}’, ‘{{d}}’, ‘{{e}}’) but instead '{{a}}', '{{b}}', '{{c}}', '{{a}}', '{{a}}'.

What I’m I doing wrong!?

I’m clueless, I appreciate your help, thank you.

EDIT:

I just tried to use an int[] and compare the id rather then the name_for_code and I’m still getting 1 and 0 appended to the last 2 elements of the array as well as the array elements are wrong:

SELECT [t4].[id], [t4].[name_for_code], [t4].[site_id], [t4].[priority_level], [t4].[value] AS [ParentId]
FROM (
    SELECT [t0].[id], [t0].[name_for_code], [t0].[site_id], [t0].[priority_level], NULL AS [value]
    FROM [dbo].[labels] AS [t0]
    WHERE ([t0].[site_id] = 15) AND ([t0].[id] IN (1, 2, 3, 4, 5))
    UNION
    SELECT [t2].[id], [t2].[name_for_code], [t2].[site_id], [t2].[priority_level], [t1].[parent_label_id] AS [value]
    FROM [dbo].[label__label] AS [t1]
    INNER JOIN [dbo].[labels] AS [t2] ON [t2].[id] = [t1].[label_id]
    WHERE EXISTS(
        SELECT NULL AS [EMPTY]
        FROM [dbo].[labels] AS [t3]
        WHERE ([t3].[id] = [t1].[parent_label_id]) AND ([t3].[site_id] = 15) AND ([t3].[id] IN (1, 2, 3, 10, 11))
        )
    ) AS [t4]
  • 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-20T12:26:28+00:00Added an answer on May 20, 2026 at 12:26 pm

    OK I found the problem and actually it was not happening when running the code but only in debug mode and specifically it seems it is a bug in Scott Gu’s LINQ to SQL Debug Visualizer

    That is why the TSQL that I was grabbing was not parameterized, @David B you made a very good point!

    I will leave a comment on Scott Gu’s page pointing to this question.

    Thanks all!

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I'm trying to create an if statement in PHP that prevents a single post
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.

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.