0
簡化示例:我有一個表,其中包含FirstName
,LastName
。我有興趣檢索所有全稱爲N的人,按照長度排序。爲了實現這一目標,我有這樣的代碼:在實體框架中選擇一個投影並按它篩選
var result = await Context.People
.Select(p => new PersonWithSalutation
{
FirstName = p.FirstName,
LastName = p.FirstName,
FullSalutation = p.FirstName + " " + p.LastName
})
.Where(p => p.FullSalutation.Length < maxLength)
.OrderBy(p => p.FullSalutation)
.Take(maxResults)
.ToListAsync();
查詢看起來是這樣的:
SELECT TOP (10)
[Project1].[C1] AS [C1],
[Project1].[Name] AS [Name],
[Project1].[Id] AS [Id],
[Project1].[C2] AS [C2]
FROM (SELECT
[Extent1].[Id] AS [Id],
[Extent1].[Name] AS [Name],
1 AS [C1],
...calculated stuff... AS [C2]
FROM [dbo].[People] AS [Extent1]
WHERE ...exactly the same stuff... <= @p__linq__3
) AS [Project1]
ORDER BY [Project1].[C2] ASC
這是卓有成效的,併產生一個單一的數據庫查詢。問題是計算投影,因爲它在結果查詢中出現兩次:一次在SELECT
,然後在WHERE
子句中出現。這個例子被簡化了,但在我的真實情況下,我正在進行沉重的數學運算,我寧願只計算一次。正如你在上面看到的,C2在order子句中被重用。我想對WHERE
條款做同樣的處理,我認爲這會涉及另一個子查詢)。我將如何實現這一目標?
我會認爲SQL Server來實現,該計算是一樣的,而不是做它在這種情況下兩次。你有理由相信它有嗎? – John