Skip to content

When using GroupBy, getting "the given key 'EmptyProjectionMember' was not present in the dictionary" #31209

@sam-wheat

Description

@sam-wheat

Possibly related to:

#20277

#22089

#20929

#30905

Exception:

   System.Collections.Generic.KeyNotFoundException: The given key 'EmptyProjectionMember' was not present in the dictionary.
   at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.EntityShaperExpression.VisitChildren(ExpressionVisitor visitor)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression)
   at Microsoft.EntityFrameworkCore.Query.SqlExpressions.SelectExpression.ApplyProjection(Expression shaperExpression, ResultCardinality resultCardinality, QuerySplittingBehavior querySplittingBehavior)
   at Microsoft.EntityFrameworkCore.Query.Internal.SelectExpressionProjectionApplyingExpressionVisitor.VisitExtension(Expression extensionExpression)
   at Microsoft.EntityFrameworkCore.Query.RelationalQueryTranslationPostprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.SqlServer.Query.Internal.SqlServerQueryTranslationPostprocessor.Process(Expression query)
   at Microsoft.EntityFrameworkCore.Query.QueryCompilationContext.CreateQueryExecutor[TResult](Expression query)
   at Microsoft.EntityFrameworkCore.Storage.Database.CompileQuery[TResult](Expression query, Boolean async)
   at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async)
   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.EntityQueryProvider.Execute[TResult](Expression expression)
   at Microsoft.EntityFrameworkCore.EntityFrameworkQueryableExtensions.ToQueryString(IQueryable source)
   at EmptyProjectionMemberTest.Program.Main(String[] args) in C:\Projects\EmptyProjectionMemberTest\EmptyProjectionMemberTest\Program.cs:line 26

Reproduce:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
	<OutputType>Exe</OutputType>
	<TargetFramework>net7.0</TargetFramework>
	<ImplicitUsings>enable</ImplicitUsings>
	<Nullable>enable</Nullable>
  </PropertyGroup>
	<ItemGroup>
		<PackageReference Include="Microsoft.EntityFrameworkCore" Version="7.0.5" />
		<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
	</ItemGroup>
</Project>




using Microsoft.EntityFrameworkCore;

namespace EmptyProjectionMemberTest;

internal class Program
{
	static async Task Main(string[] args)
	{
		DbContextOptionsBuilder builder = new();
		builder.UseSqlServer("Data Source=.;Initial Catalog=EmptyProjectionMemberTest;Integrated Security=True;MultipleActiveResultSets=True;Encrypt=false");
		Db db = new(builder.Options);
		await db.Database.EnsureDeletedAsync();
		await db.Database.EnsureCreatedAsync();

		try
		{
			var query = db.ReleaseDates
				.GroupBy(x => new { x.ReleaseID, x.DateReleased })
				.Select(x => x.First())
				.GroupBy(x => x.ReleaseID);

			string sql = query.ToQueryString(); // fails here

			var stuff = query.ToListAsync();
		}
		catch(Exception ex) 
		{
			Console.WriteLine(ex.ToString()); // The given key 'EmptyProjectionMember' was not present in the dictionary.
		}
	}
}


public class Db : DbContext
{
	public Db(DbContextOptions options) : base(options) { }

	public DbSet<ReleaseDate> ReleaseDates { get; set; }
}

public class ReleaseDate
{
	public int ID { get; set; }
	public string ReleaseID { get; set; }
	public DateTime DateReleased { get; set; }
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No fields configured for Bug.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions