Entity Framework allows to gather two result sets into one large with a Union method. Union would make a unique list of items. A simple LINQ query could look like the following:
1 2 3 4 5 |
var query15 = ctx.Clients .Select(c => c.CountryCode) .Union( ctx.Phones.Select(p => p.CountryCode) ); |
Entity Framework generates a bit strange T-SQL code with SELECT DISTINCT and UNION ALL instead of an obvious UNION operator.
1 2 3 4 5 6 7 8 9 10 11 12 |
SELECT [Distinct1].[C1] AS [C1] FROM ( SELECT DISTINCT [UnionAll1].[CountryCode] AS [C1] FROM (SELECT [Extent1].[CountryCode] AS [CountryCode] FROM [dbo].[Clients] AS [Extent1] UNION ALL SELECT [Extent2].[CountryCode] AS [CountryCode] FROM [dbo].[Phones] AS [Extent2]) AS [UnionAll1] ) AS [Distinct1] |
This query do the same.
1 2 3 4 5 |
SELECT CountryCode FROM [dbo].[Clients] UNION SELECT CountryCode FROM [dbo].[Phones] |
It’s more clear and concise, but if you look at execution plans there will no difference.