Is the Linq "Contain" static function corrupted by its contents on a subquery?

I am trying to reduce the number of database queries, and in doing so I am trying to get a series of hierarchical objects (which were previously selected recursively) at a time.

I have a property Labelscontaining a rowset:

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

which I use to build the 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 checked during debugging, will generate the next 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 declaration IDeferredToplabels, I extend the first request with Union, defining how the child objects should be received Label:

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 check TSQL, do the following:

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]

TSQL, IN ('{{a}}', '{{b}}', '{{c}}', '{{a}}'0, '{{a}}'1)), 0 1 ( .. {{a}} ', {{b}}', '{{c}}', '{{d}}', '{{e}}'), '{{a}}', '{{b}}', '{{c}}', '{{a}}', '{{a}}'.

?

, , .

EDIT:

int[] id, name_for_code, 1 0, 2 ,

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]
+3
2

OK , , , , , Scott Gu LINQ SQL Debug Visualizer

TSQL, , , @David B !

, .

!

+2

Labels (). EG:

string[] LabelsCopy = new string[5];
Labels.CopyTo(LabelsCopy, 0);

( Console.WriteLine(String.Join(',',Labels))).

, .Contains() Labels, , .

0
source

All Articles