I have a query which is fully translatable to SQL. For unknown reasons LINQ decides the last Select()
to execute in .NET (not in the database), which causes to run a lot of additional SQL queries (per each item) against database.
Actually, I found a strange way to force the full translation to SQL:
我有点问(这是一份真正简化的文本,但并未按预期运作):
MainCategories.Select(e => new
{
PlacementId = e.CatalogPlacementId,
Translation = Translations.Select(t => new
{
Name = t.Name,
// ...
}).FirstOrDefault()
})
该科将提出许多问题:
SELECT [t0].[CatalogPlacementId] AS [PlacementId]
FROM [dbo].[MainCategories] AS [t0]
SELECT TOP (1) [t0].[Name]
FROM [dbo].[Translations] AS [t0]
SELECT TOP (1) [t0].[Name]
FROM [dbo].[Translations] AS [t0]
...
但是,如果我再附上一份<代码>Select(,则仅复制所有成员:
.Select(e => new
{
PlacementId = e.PlacementId,
Translation = new
{
Name = e.Translation.Name,
// ...
}
})
它将汇编成一份单一的结构说明:
SELECT [t0].[CatalogPlacementId] AS [PlacementId], (
SELECT [t2].[Name]
FROM (
SELECT TOP (1) [t1].[Name]
FROM [dbo].[Translations] AS [t1]
) AS [t2]
) AS [Name]
FROM [dbo].[MainCategories] AS [t0]
任何污点为何? How to force the LINQkou to make a one query more generalally (不含第二版Select(
)?
<>光> 我提出询问,使之真正简单。
PS: Only, idea I get is to post-process/transform queries with similar patterns (to add the another Select()
).