表的查询性能在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
接口来获得同样的好处,因为后者的例子。