dotnet / dotnet/efcore

Provide a way to translate to Sum() over short (and other similar scenarios)

Open
#34,462 6 comments 1 reaction 0 assignees View on GitHub
area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

## File a bug

Failing to project query with sum of subquery that select short properties to get totals. with int properties work properly and not work id cast type.

```C#
public class Piece {
public int Id { get; set; }
public string Name { get; set; }
public short Qty { get; set; }
}

public class Assembly {
public int Id { get; set; }
public string Name { get; set; }
public short Qty { get; set; }
}

public class AssemblyPart {
public int AssemblyId { get; set; }
public int PieceId { get; set; }
public short Qty { get; set; }
}

var query1 = await (from a in db.Assemblies
select new {
a.Id,
a.Name,
Parts = (from p in db.AssemblyParts
where p.AssemplyId == a.Id
select p.Qty).Sum()
}).ToListAsync();

var query2 = await (from a in db.Assemblies
select new {
a.Id,
a.Name,
Parts = (from ap in db.AssemblyParts
join p in db.Pieces on ap.PieceId equal p.Id
where a.AssemplyId == a.Id
select ap.Qty * p.Qty).Sum()
}).ToListAsync();
```

### Stack traces

```
System.Collections.Generic.KeyNotFoundException: The given key 'System.Int16' was not present in the dictionary.
at System.Collections.Generic.Dictionary`2.get_Item(TKey key)
at Microsoft.EntityFrameworkCore.Query.QueryableMethods.GetSumWithoutSelector(Type type)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateAggregateWithSelector(ShapedQueryExpression source, LambdaExpression selectorLambda, Func`2 methodGenerator, Boolean throwWhenEmpty, Type resultType)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.TranslateSum(ShapedQueryExpression source, LambdaExpression selector, Type resultType)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalQueryableMethodTranslatingExpressionVisitor.Translate(Expression expression)
at Microsoft.EntityFrameworkCore.Query.QueryableMethodTranslatingExpressionVisitor.TranslateSubquery(Expression expression)
at Microsoft.EntityFrameworkCore.Query.RelationalSqlTranslatingExpressionVisitor.VisitMethodCall(MethodCallExpression methodCallExpression)
```

### Provider and version information

EF Core version: 8.0.7
Database provider: Microsoft.EntityFrameworkCore.SqlServer,Npgsql.EntityFrameworkCore.PostgreSQL
Target framework: .NET 8.0
Operating system: Windows 11
IDE: VS Code

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.