dotnet / dotnet/efcore

GroupBy nullable column followed by First() over group returns null record

Open
#29,240 0 comments 0 reactions 0 assignees View on GitHub
area-groupby area-query customer-reported
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

```
var query = applicationDbContext.Categories
.GroupBy(x => x.GroupId)
.Select(x => x.OrderByDescending(x => x.CreationDateTime).First());
```
Result:
![GroupByNullable](https://user-images.githubusercontent.com/17910491/193207798-e3032812-1b0e-4a8d-809e-cfc410ea6df9.png)

Expected result:
![GroupByNullable_COALESCE](https://user-images.githubusercontent.com/17910491/193207872-a55842d4-770e-40e9-b56b-006a6f04c1d7.png)

Workaround:
```
var query = applicationDbContext.Categories
.GroupBy(x => x.GroupId ?? 0)
.Select(x => x.OrderByDescending(x => x.CreationDateTime).First());
```

Reproduction repo:
https://github.com/alienwareone/EfCoreGroupByNullable

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.