Suppose I have a list of strings that is sent with a client, in this case regions. And I have a table called Compliance Regions. I want to find strings that have a name property, and a region element must exist in the string.
In LINQ, I can do this in two different ways. As shown below, they create two different SQL queries. My question is which one to choose? Which query has the best performance?
List<string> regions = new List<string>() { "Canada", "EN 50530" }; var cregions = from c in complianceRegions from r in regions where c.Name.Equals(r) select c; var cregions2 = from c in complianceRegions where regions.Any(x => x == c.Name) select c;
The generated sql is shown below.
-- cregions SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent1].[Description] AS [Description] FROM [Administration].[ComplianceRegions] AS [Extent1] INNER JOIN (SELECT N'Canada' AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable1] UNION ALL SELECT N'EN 50530' AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1] ON [Extent1].[Name] = [UnionAll1].[C1]

AND
--cregions2 SELECT [Extent1].[Id] AS [Id], [Extent1].[Name] AS [Name], [Extent1].[Description] AS [Description] FROM [Administration].[ComplianceRegions] AS [Extent1] WHERE EXISTS (SELECT 1 AS [C1] FROM (SELECT N'Canada' AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable1] UNION ALL SELECT N'EN 50530' AS [C1] FROM ( SELECT 1 AS X ) AS [SingleRowTable2]) AS [UnionAll1] WHERE [UnionAll1].[C1] = [Extent1].[Name] )
Added execution plan on request.
source share