dotnet / dotnet/efcore

KeyNotFoundException during ApplyProjection phase of a groupby query with FirstOrDefault

Open
#33,216 2 comments 1 reaction 0 assignees View on GitHub
area-groupby area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

```cs
[ConditionalTheory]
[MemberData(nameof(IsAsyncData))]
public virtual Task MyTest(bool async)
=> AssertQuery(
async,
ss => from g in ss.Set().GroupBy(
x => x.Orders.Where(x => x.OrderDate.Value.Month == 2).Count(),
(k, g) => new { FebOrderCount = k, FirstCustomer = g.OrderBy(x => x.CustomerID).FirstOrDefault() })
select new { g.FebOrderCount, g.FirstCustomer.CustomerID });
```

exception:

```
System.Collections.Generic.KeyNotFoundException : The given key 'EmptyProjectionMember' was not present in the dictionary.

Stack Trace: 
Dictionary`2.get_Item(TKey key)
ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression) line 112
StructuralTypeShaperExpression.VisitChildren(ExpressionVisitor visitor) line 207
ProjectionMemberToIndexConvertingExpressionVisitor.Visit(Expression expression) line 118
SelectExpression.ApplyProjection(Expression shaperExpression, ResultCardinality resultCardinality, QuerySplittingBehavior querySplittingBehavior) line 742
SelectExpressionProjectionApplyingExpressionVisitor.VisitExtension(Expression extensionExpression) line 39
RelationalQueryTranslationPostprocessor.Process(Expression query) line 43
SqlServerQueryTranslationPostprocessor.Process(Expression query) line 47
QueryCompilationContext.CreateQueryExecutor[TResult](Expression query) line 165
Database.CompileQuery[TResult](Expression query, Boolean async) line 66
QueryCompiler.CompileQueryCore[TResult](IDatabase database, Expression query, IModel model, Boolean async) line 126
<>c__DisplayClass11_0`1.b__0() line 82
```

possibly similar cause to https://github.com/dotnet/efcore/issues/33215 (?)

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.