Skip to content

Concatenating multiple IQueryable<T>'s causes "EmptyProjectionMember" exception on the InMemory database #30905

@NinovanderMark

Description

@NinovanderMark

When concatenating multiple IQueryable<T>'s, where one refers to a DbSet<T> and the other simply projects to a compatible IQueryable<T>, I get a KeyNotFoundException stating that The given key 'EmptyProjectionMember' was not present in the dictionary.

I'm unable to determine what part of my query is the issue, either query works fine separately. The reason I want to concatenate them as such, is so that I can apply paging, sorting and filtering on the combined set, as though they were the same entity.

This may not actually be a supported operation, I noticed that using a different provider (for our SQL server) causes problems as well (Unable to translate set operation after client projection has been applied).

I've searched for similar issues, but found only these issues for older EF Core versions, which are already closed, and may not be related.
#20277
#22089

Minimal example code

Here's a simple example project that reproduces the issue I'm seeing:
https://github.com/NinovanderMark/EmptyProjectionMember-Reproduction

My guess is that there's something about combining these queries that doesn't quite work out.

// Running queries on the data
var carList = dbContext.Set<Car>().AsQueryable();
var oldCarList = from car in dbContext.Set<OldCar>()
				 .Include(c => c.Driver)
				 select new Car
				 {
					 Id = car.Id,
					 Brand = car.CarBrand,
					 DriverName = car.Driver.Name
				 };

var combinedList = carList.Concat(oldCarList);
var result = combinedList.ToList(); // <= Exception occurs here

Stacktrace

The given key 'EmptyProjectionMember' was not present in the dictionary.
   at System.ThrowHelper.ThrowKeyNotFoundException[T](T key)
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryQueryExpression.GetProjection(ProjectionBindingExpression projectionBindingExpression)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.VisitBinary(BinaryExpression binaryExpression)
   at System.Linq.Expressions.BinaryExpression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at System.Dynamic.Utils.ExpressionVisitorUtils.VisitBlockExpressions(ExpressionVisitor visitor, BlockExpression block)
   at System.Linq.Expressions.ExpressionVisitor.VisitBlock(BlockExpression node)
   at System.Linq.Expressions.BlockExpression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.ShaperExpressionProcessingExpressionVisitor.ProcessShaper(Expression shaperExpression)
   at Microsoft.EntityFrameworkCore.InMemory.Query.Internal.InMemoryShapedQueryCompilingExpressionVisitor.VisitShapedQuery(ShapedQueryExpression shapedQueryExpression)
   at Microsoft.EntityFrameworkCore.Query.ShapedQueryCompilingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at System.Linq.Expressions.Expression.Accept(ExpressionVisitor visitor)
   at System.Linq.Expressions.ExpressionVisitor.Visit(Expression node)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass9_0`1.<Execute>b__0()
   at Microsoft.EntityFrameworkCore.Query.Internal.CompiledQueryCache.GetOrAddQuery[TResult](Object cacheKey, Func`1 compiler)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.Execute[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Query.Internal.EntityQueryable`1.GetEnumerator()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at EmptyProjectionMember_Reproduction.Program.Main(String[] args) in D:\Code\GitHub\EmptyProjectionMember-Reproduction\Program.cs:line 39

Provider and version information

EF Core version: 6.0.16
Database provider: Microsoft.EntityFrameworkCore.InMemory
Target framework: .NET 6.0
Operating system: Windows 10 22H2
IDE: Visual Studio 2022 17.5.3

Activity

ajcvickers

ajcvickers commented on May 16, 2023

@ajcvickers
Contributor

Note for triage: still repros on latest daily build.

added this to the Backlog milestone on May 16, 2023
NinovanderMark

NinovanderMark commented on May 23, 2023

@NinovanderMark
Author

We've been trying to work around this issue and found that doing AsEnumerable() for both queries, and concatenating those instead, works as intended.

This isn't much of an issue for the in-memory database, but it is when using these same queries to access our SQL database, which also doesn't work as mentioned.

NinovanderMark

NinovanderMark commented on May 23, 2023

@NinovanderMark
Author

After some more attempts to work around it, I came across #16243 which led me to change the query to the following, which works fine.

var combinedList =
	dbContext.Cars.Select(c => new { c.Id, c.Brand, c.DriverName })
	.Union(dbContext.OldCars.Select(c => new { c.Id, Brand = c.CarBrand, DriverName = c.Driver.Name }))
	.Select(c => new Car
	{
		Id = c.Id,
		Brand = c.Brand,
		DriverName = c.DriverName
	});
roji

roji commented on Jul 8, 2023

@roji
added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    Projects

    No projects

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @ajcvickers@roji@cincuranet@NinovanderMark

        Issue actions

          Concatenating multiple IQueryable<T>'s causes "EmptyProjectionMember" exception on the InMemory database · Issue #30905 · dotnet/efcore