我正在使用 Linq to SQl 来查询数据。当我在 SQL Server Profiler 中使用 order by 和监视编写选择查询时,它会运行选择命令 2 次
- 从表中选择字段
- 订购数据。
数据量大需要时间。有什么解决方案。
更新:
from dc in dataContext.UserTable join
m in dataContext.MonthLookups on dc.Month equals m.Month into sr
from x in sr.DefaultIfEmpty()
order by dc.UserName, dc.FirstName
select new {dc, sr.MothName};
更新:
SELECT
[Project1].[ UserName] AS [UserName],
[Project1].[ FirstName] AS [FirstName],
[Project1].[ MonthName] AS [MonthName],
[Project1].[year] AS [year]
FROM ( SELECT
[Extent1].[UserName] AS [UserName],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[year] AS [year],
[Extent2].[MonthName] AS [MonthName]
FROM [dbo].[DutySavingFin] AS [Extent1]
LEFT OUTER JOIN [dbo].[MonthLookup] AS [Extent2] ON [Extent1].[Month] = [Extent2].[Month]
) AS [Project1]
ORDER BY [Project1].[UserName] ASC, [Project1].[FirstName] ASC
更新 :
首选方式:
SELECT
[Extent1].[UserName] AS [UserName],
[Extent1].[FirstName] AS [FirstName],
[Extent1].[year] AS [year],
[Extent2].[MonthName] AS [MonthName]
FROM [dbo].[DutySavingFin] AS [Extent1]
LEFT OUTER JOIN [dbo].[MonthLookup] AS [Extent2] ON [Extent1].[Month] = [Extent2].[Month]
ORDER BY [Extent1].[UserName] ASC, [Extent1].[FirstName] ASC