Suppose, I have a list of string which is sent from client, in this case regions. And I have a table named Compliance Regions. I want to search for the rows which has a name property and region item should be exist on the row.
In the LINQ, I can do it in two different ways. Like below, and they produce two different SQL Queries. My question is which should I choose? Which query has the better 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 showed 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]
)