表的查詢性能在LINQ to SQL類,爲什麼屬性是從外鍵創建EntitySet
對象,實施IEnumerable
,其中作爲對DataContext
的對象是其實現IQueryable
Table
對象?的EntitySet VS在LINQ2SQL
編輯:爲了澄清,這裏是一個例子,說明我試圖理解。這個例子:
ctx.Matches.Where(x => x.MatchID == 1).Single()
.MatchPlayers.Max(x => x.Score);
訪問數據庫的兩倍,其中如:
ctx.MatchPlayers.Where(x => x.MatchID == 1)
.Max(x => x.Score);
只能運行1個查詢。這裏有痕跡:
exec sp_executesql N'SELECT [t0].[MatchID], [t0].[Date]
FROM [dbo].[Matches] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1
go
exec sp_executesql N'SELECT [t0].[MatchID], [t0].[PlayerID], [t0].[Score]
FROM [dbo].[MatchPlayers] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1
go
和
exec sp_executesql N'SELECT MAX([t0].[Score]) AS [value]
FROM [dbo].[MatchPlayers] AS [t0]
WHERE [t0].[MatchID] = @p0',N'@p0 int',@p0=1
go
這也表明,更糟的是,最大是在C#的水平,而不是在數據庫中完成的。
我知道,發生這種情況的原因是IQueryable
S和IEnumerable
S之間的差別,所以爲什麼不MatchPlayers
對象在第一個示例實現IQueryable
接口來獲得同樣的好處,因爲後者的例子。