我正在使用entityframework,我有2个模型:
我的目标是在表GeographyBoundary中获取不同的CountryISO3166 ID。
我使用我的dataContext(db)解析了数据库中的数据,我试图首先使用 IQueryable 获取数据:
IQueryable<Int64> listCountryId = db.GeographyBoundary
.Select(x => x.CountryId).Distinct();
IQueryable<CountryISO3166> results = db.CountryISO3166
.Where(x=> listCountryId.Any(y=> y == x.Id));
foreach (var result in results)
{
Console.WriteLine("Result countryId: " + result.Name_en);
}
然后我对列表:
做了同样的事情List<Int64> listCountryId = db.GeographyBoundary
.Select(x => x.CountryId).Distinct();
List<CountryISO3166> results = db.CountryISO3166
.Where(x=> listCountryId.Any(y=> y == x.Id));
foreach (var result in results)
{
Console.WriteLine("Result countryId: " + result.Name_en);
}
使用 IQueryable
调用db时SQL Profiler中的数据SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name_en] AS [Name_en],
[Extent1].[Name_fr] AS [Name_fr],
[Extent1].[ISO3166_1_alpha_2] AS [ISO3166_1_alpha_2],
[Extent1].[ISO3166_1_alpha_3] AS [ISO3166_1_alpha_3],
[Extent1].[ISO3166_1_numeric] AS [ISO3166_1_numeric],
[Extent1].[Itu] AS [Itu],
[Extent1].[Marc] AS [Marc],
[Extent1].[Wmo] AS [Wmo],
[Extent1].[Ds] AS [Ds],
[Extent1].[Dial] AS [Dial],
[Extent1].[Fifa] AS [Fifa],
[Extent1].[Fips] AS [Fips],
[Extent1].[Gaul] AS [Gaul],
[Extent1].[Ioc] AS [Ioc],
[Extent1].[CurrencyAlphabeticCode] AS [CurrencyAlphabeticCode],
[Extent1].[CurrencyCountryName] AS [CurrencyCountryName],
[Extent1].[CurrencyMinorUnit] AS [CurrencyMinorUnit],
[Extent1].[CurrencyName] AS [CurrencyName],
[Extent1].[CurrencyNumericCode] AS [CurrencyNumericCode],
[Extent1].[IsIndependent] AS [IsIndependent]
FROM [dbo].[CountryISO3166] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM ( SELECT DISTINCT
[Extent2].[CountryId] AS [CountryId]
FROM [dbo].[GeographyBoundary] AS [Extent2]
) AS [Distinct1]
WHERE [Distinct1].[CountryId] = [Extent1].[Id]
)
使用列表
调用db时,SQL事件探查器中的数据SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name_en] AS [Name_en],
[Extent1].[Name_fr] AS [Name_fr],
[Extent1].[ISO3166_1_alpha_2] AS [ISO3166_1_alpha_2],
[Extent1].[ISO3166_1_alpha_3] AS [ISO3166_1_alpha_3],
[Extent1].[ISO3166_1_numeric] AS [ISO3166_1_numeric],
[Extent1].[Itu] AS [Itu],
[Extent1].[Marc] AS [Marc],
[Extent1].[Wmo] AS [Wmo],
[Extent1].[Ds] AS [Ds],
[Extent1].[Dial] AS [Dial],
[Extent1].[Fifa] AS [Fifa],
[Extent1].[Fips] AS [Fips],
[Extent1].[Gaul] AS [Gaul],
[Extent1].[Ioc] AS [Ioc],
[Extent1].[CurrencyAlphabeticCode] AS [CurrencyAlphabeticCode],
[Extent1].[CurrencyCountryName] AS [CurrencyCountryName],
[Extent1].[CurrencyMinorUnit] AS [CurrencyMinorUnit],
[Extent1].[CurrencyName] AS [CurrencyName],
[Extent1].[CurrencyNumericCode] AS [CurrencyNumericCode],
[Extent1].[IsIndependent] AS [IsIndependent]
FROM [dbo].[CountryISO3166] AS [Extent1]
WHERE EXISTS (SELECT
1 AS [C1]
FROM (SELECT
cast(2 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable1]
UNION ALL
SELECT
cast(5 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable2]
UNION ALL
SELECT
cast(10 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable3]
UNION ALL
SELECT
cast(13 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable4]
UNION ALL
SELECT
cast(14 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable5]
UNION ALL
SELECT
cast(20 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable6]
UNION ALL
SELECT
cast(21 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable7]
UNION ALL
SELECT
cast(26 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable8]
UNION ALL
SELECT
cast(28 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable9]
UNION ALL
SELECT
cast(29 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable10]
UNION ALL
SELECT
cast(31 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable11]
UNION ALL
SELECT
cast(34 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable12]
UNION ALL
SELECT
cast(39 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable13]
UNION ALL
SELECT
cast(45 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable14]
UNION ALL
SELECT
cast(54 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable15]
UNION ALL
SELECT
cast(58 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable16]
UNION ALL
SELECT
cast(60 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable17]
UNION ALL
SELECT
cast(69 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable18]
UNION ALL
SELECT
cast(74 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable19]
UNION ALL
SELECT
cast(75 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable20]
UNION ALL
SELECT
cast(82 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable21]
UNION ALL
SELECT
cast(84 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable22]
UNION ALL
SELECT
cast(85 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable23]
UNION ALL
SELECT
cast(100 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable24]
UNION ALL
SELECT
cast(101 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable25]
UNION ALL
SELECT
cast(102 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable26]
UNION ALL
SELECT
cast(106 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable27]
UNION ALL
SELECT
cast(109 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable28]
UNION ALL
SELECT
cast(111 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable29]
UNION ALL
SELECT
cast(114 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable30]
UNION ALL
SELECT
cast(117 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable31]
UNION ALL
SELECT
cast(118 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable32]
UNION ALL
SELECT
cast(122 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable33]
UNION ALL
SELECT
cast(127 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable34]
UNION ALL
SELECT
cast(128 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable35]
UNION ALL
SELECT
cast(129 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable36]
UNION ALL
SELECT
cast(131 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable37]
UNION ALL
SELECT
cast(137 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable38]
UNION ALL
SELECT
cast(143 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable39]
UNION ALL
SELECT
cast(145 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable40]
UNION ALL
SELECT
cast(146 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable41]
UNION ALL
SELECT
cast(148 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable42]
UNION ALL
SELECT
cast(156 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable43]
UNION ALL
SELECT
cast(158 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable44]
UNION ALL
SELECT
cast(161 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable45]
UNION ALL
SELECT
cast(165 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable46]
UNION ALL
SELECT
cast(167 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable47]
UNION ALL
SELECT
cast(173 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable48]
UNION ALL
SELECT
cast(174 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable49]
UNION ALL
SELECT
cast(176 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable50]
UNION ALL
SELECT
cast(177 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable51]
UNION ALL
SELECT
cast(180 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable52]
UNION ALL
SELECT
cast(181 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable53]
UNION ALL
SELECT
cast(192 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable54]
UNION ALL
SELECT
cast(196 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable55]
UNION ALL
SELECT
cast(199 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable56]
UNION ALL
SELECT
cast(201 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable57]
UNION ALL
SELECT
cast(202 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable58]
UNION ALL
SELECT
cast(205 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable59]
UNION ALL
SELECT
cast(208 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable60]
UNION ALL
SELECT
cast(214 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable61]
UNION ALL
SELECT
cast(215 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable62]
UNION ALL
SELECT
cast(217 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable63]
UNION ALL
SELECT
cast(220 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable64]
UNION ALL
SELECT
cast(227 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable65]
UNION ALL
SELECT
cast(232 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable66]
UNION ALL
SELECT
cast(234 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable67]
UNION ALL
SELECT
cast(235 as bigint) AS [C1]
FROM ( SELECT 1 AS X ) AS [SingleRowTable68]) AS [UnionAll67]
WHERE [UnionAll67].[C1] = [Extent1].[Id]
)
结果是:
为什么会这样?
不应该更快IQuery?