-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Query: Projecting second level collection navigation using First() generates invalid SQL #8823
Comments
Can you fetch SQL, EF sent for the query? |
I was following instructions from here and get this results.
and same in SQL Server Profiler:
Hope it helps. |
I want to write this sql with EF:
|
This repros in 1.1.2 release. Few notes:
Based on SQL you posted above (which select Title instead of Parts-Title_Translate, notice difference between a domain class & string property) this query should give you correct results. (it works in 1.1.2 version also) var result = db.Parts
.Select(p => new
{
id = p.Id,
translate = p.Titles.OrderByDescending(t => t.IsDefault)
.Select(t => t.Translates.Where(l => l.LangISOCode == lang).Select(l => l.Title).FirstOrDefault()).FirstOrDefault()
}); Generated SQL: SELECT [p].[Id], (
SELECT TOP(1) (
SELECT TOP(1) [l].[Title]
FROM [Translates] AS [l]
WHERE ([l].[LangISOCode] = @__lang_0) AND ([t].[Id] = [l].[TitleId])
)
FROM [Titles] AS [t]
WHERE [p].[Id] = [t].[PartId]
ORDER BY [t].[IsDefault] DESC
) AS [translate]
FROM [Parts] AS [p] |
I am trying to query all parts, then there first title and after that Title field from Parts-Title_Translates.
This is my table relationship.
Exception:
Intellisense shows correct fields.
Steps to reproduce
Further technical details
EF Core version: 1.1.2
Database Provider: Microsoft.EntityFrameworkCore.SqlServer
Operating system: Win 10, Database: win 7 SQL Server 2016
IDE: Visual Studio 2017 Preview 2 (from About: 15.3.0 PReview 2.0)
Notes:
I also try with this query (which works)
but it takes a few minutes to fetch results, because it fetches one by one. I need a query to fetch with one round-trip to database.
The text was updated successfully, but these errors were encountered: