Projection mapping is not updated when set operation within pending collection
See original GitHub issueI am not getting correct result when using “Union” inside the select of objects in Linq query.
My requirement is that I need to match mapper table and get the details by combining two column values into single and return to the object inside “select” statement in linq query. Hence I used “Union” in this scenario.
Sample Query:
var resultCollection = (from studentGroup in context.StudentGameMapper
where studentGroup.GroupId==6
select new StudentGameResult
{
GroupId= studentGroup.GroupId
StudentId= studentGroup.StudentId
SportsList= (from inDoorSports in Context.InDoorSports
&& inDoorSports.Id == studentGroup.InCategoryId
select inDoorSports.Name)
.Union(from outDoorSports in Context.OutDoorSports
&& outDoorSports.Id == studentGroup.OutCategoryId
select outDoorSports.Name).ToList()
})).AsQueryable();
For each student, I am getting either first value of “InDoorSports” or “OutDoorSports” even if the student have value for both the sports category column.
Note: If I directly fetch the result of sport name alone by using “Union” I can get full values. But when used “Union” inside “select” statement object I am not getting expected value.
var resultCollection = (from inDoorSports in Context.InDoorSports && inDoorSports.Id == 1
select inDoorSports.Name)
.Union(
from outDoorSports in Context.OutDoorSports
&& outDoorSports.Id == 1
select outDoorSports.Name).AsQueryable();
Kindly let me know is this an issue or anything wrong in my query.
Further technical details EF Core version: 3.0.0 Database provider: Npgsql.EntityFrameworkCore.PostgreSQL Target framework: .NET Core 3.0 IDE: Visual Studio 2019 16.3.5
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (5 by maintainers)
Track separately.
After some investigation, this looks like a dup of #17337 (loss of referential integrity).
The projection with the union is in the pending collections. When CollectionJoinApplyingEV processes the query, it pushes down the query before applying the CROSS APPLY (as there’s a Limit). As part of this, the pending collections are remapped with SqlRemappingVisitor, to modify the ColumnExpressions referring to the query being pushed down. As a result, the UnionExpression in the pending collection’s SelectExpression changes; but nobody changes that SelectExpression’s projection mapping, so the column continues to refer to the old UnionExpression. As a result it has the wrong alias.
Learned quite a lot from this one…