Chained select clauses, last one disregarded
See original GitHub issueWe have reproduced this issue in EF Core 2.2 and 3.1, on both MSSQL and Oracle (Devart provider)
I am not sure whether we are doing something wrong, or have something configured incorrectly or whether it’s an EF Core bug.
We have the following IQueryable
:
var query = _context.EntityDbSet
.OrderBy(nameof(Entity.StartDate))
.Select(e => new Entity
{
Id = e.Id,
RelatedItemId = e.RelatedItemId,
StartDate = e.StartDate,
EndDate = e.EndDate ...
})
.Skip(0).Take(10)
We then proceed to use this query in several other queries.
The Id
field is the Key in this table.
The issue I have is as follows:
var relatedItems = query.Select(e => e.RelatedItemId).ToList();
Generates the following SQL:
SELECT [e].[RELATED_ITEM_ID]
FROM [ENTITY] AS [e]
ORDER BY [e].[START_DATE]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY
This works as expected. But the following doesn’t:
var ids = query.Select(e => e.Id).ToList();
Generates the following SQL:
SELECT [e].[ID], [e].[RELATED_ITEM_ID], [e].[START_DATE], [e].[END_DATE] ...
FROM [ENTITY] AS [e]
ORDER BY [e].[START_DATE]
OFFSET @__p_0 ROWS FETCH NEXT @__p_1 ROWS ONLY
I use ids.Contains
in a different query. Obviously this fails, because you must only select a single field to use this query in an IN
SQL clause.
I tried putting the original Select cause at the end of the var query = … part of the code. If I completely remove this original select, then it works as expected, selecting only the Id field.
Not sure if this has anything to do with it, but we use a custom value generator for the Id column,
Further technical details
EF Core version: Database provider: Microsoft.EntityFrameworkCore.SqlServer Target framework: .NET Core 2.2, 3.1) Operating system: IDE: Visual Studio 2019 16.3
Issue Analytics
- State:
- Created 4 years ago
- Comments:10 (6 by maintainers)
@arthur-liberman unfortunately our daily builds feed isn’t in a great state at the moment… You want to use the latest packages with version 5.0.0-alpha.1.* (note the dot after the alpha!). Your nuget.config should look something like the following:
@arthur-liberman we discussed it within the team, but unfortunately this doesn’t meet our bar for 3.1.x patch - it only seems to affect the dynamic expression generation, which is quite niche/advanced scenario. So the issue will only be officially fixed in the 5.0 timeframe.