Repository navigation
Description
Possibly related to:
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; }
}
Reacted by Matyáš Bach, Kevin Petit, Kevin Hahn, James Gurung, GorudoYami, Robin Sue, Karol Stosik, Krzysztof Poltrok, ahmetsekmen, M. Scott Ford and 31 more
Activity
hahn-kev commentedon Oct 16, 2023
I'm also seeing this, my query is a little different though.
the second Select breaks everything, without the second select it works fine. This is a simplification but it has the same issue.
ZoySoy commentedon Nov 10, 2023
Hello. I also could reproduce "The given key 'EmptyProjectionMember' was not present in the dictionary" exception but on the little bit different example. I faced with this problem during migration from EntityFramework 6.4.4 to EF core. On EF 6.4.4 this query worked well. I have prepared similar repro example based on default blog/post example. There is failed query example:
For this particular case the problem can be solved by moving Where(...) method before GroupBy(...), but maybe this case will help you to find initial problem anyway because, as I mentioned, it worked previously and I don't think that it is such an unusual query which shouldn't work.
Frameworks:
net6.0
EntityFrameworkCore - 7.0.13
Thanks in advance.
lucasbode commentedon May 3, 2024
I encountered a similar error with a query like this...
It looks silly, but we were able to work around it by doing this...
lvyyln commentedon Jul 3, 2024
roji commentedon Jul 3, 2024
kwakubiney commentedon Jul 12, 2024
Facing similar issue with this:
EFCore 7.0.0
.NET 6
Khungersumit commentedon Aug 10, 2024
Okay, so here is the discussion about this issue. I am having the same issue.
query = groupBy(c=>some el)
Select(g => g.OrderByDescending(m => m.CreatedAt).FirstOrDefault())
// Culprit
query= query.OrderByDescending(c => c.CreatedAt);
This additional sorting operation seems to cause problems. Without this final sort, everything works as expected. I've spent hours trying to figure it out and even tried writing a raw SQL query, but that didn’t solve the problem either. I'm stumped! 🤔
Khungersumit commentedon Aug 10, 2024
Unfortunately, this workaround didn't pan out for me.
TimFeldmann-Work commentedon Aug 21, 2024
I am also running into this exact issue and spent the past 5 hours trying to find a workaround with no success.
Khungersumit commentedon Aug 21, 2024
can you share your query?
9 remaining items
RichardD2 commentedon Jun 6, 2025
Scratch that workaround - I've just discovered that it actually breaks the projection!
My projection loads the value of navigation properties from related entities. But when it's followed by the
GroupBy, those properties are not included in the generated query, and are set tonull.Eg:
uses the query:
This query includes all of the columns from the
ActivityLimitentity, and none of the columns from the projection! TheActivityNameon the projection isnullfor every row.It seems the real workaround is to rewrite the query as:
This uses the query:
which correctly populates the projection.
dec123ac commentedon Aug 22, 2025
I am seeing what appears to be a variation on this defect. I receive this error message for an 'EmptyProjectionMember' even though I'm not expressly using a GroupBy() or OrderBy.
I put together three examples. The first two work as expected by selecting the object directly and putting it in a list or selecting the object then putting it into an anonymous object and then tolist.
The third LINQ gives the error. All it is doing differently is checking to see if the query's selected object is null but it should not be and there is not a null pointer exception along with it.
roji commentedon Aug 23, 2025
@dec123ac please open a new issue with a runnable, minimal repro - a query fragment such as what you posted above isn't sufficient, and it's likely your error is unrelated due to the lack of GroupBy.
[-]The given key 'EmptyProjectionMember' was not present in the dictionary[/-][+]When using GroupBy, getting "the given key 'EmptyProjectionMember' was not present in the dictionary"[/+]henriquewr commentedon Apr 20, 2026
For everyone hitting this issue or any issue related to GroupBy (at least in the cases that came to mind right now) I found a workaround:
dbSet.GroupBy(x => x.GroupByKey)can be rewritten as
Example:
becomes
Fix GroupBy.Select EmptyProjectionMember (#38140)
Fix GroupBy().Select(entity) followed by a second projection (dotnet#…
Fix GroupBy().Select(entity) followed by a second projection (#38436)