dotnet / dotnet/efcore

No items when projecting out groupings with a null key

Open
#32,026 11 comments 0 reactions 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

Hello!

I have a code which I expect to return me 3 groups of items.

```C#
var spec = new EntitySpec();
var query = _repository.GetQuery(spec);

var result = await query
.GroupBy(x => (Guid?)x.Json.RootElement.GetProperty(property).GetGuid())
.Select(g => new { g.Key, Count = g.Count(), Items = g.ToList() })
.ToListAsync(cancellationToken);
```

It really returns me groups, but unfortunately the result is incomplete:
```json
[
{
"key": "d0005e8f-ebaa-481d-aabf-628b5d172c05",
"count": 2,
"items": [
"",
""
]
},
{
"key": "f6223371-aa16-4e51-94c7-f91af84b2200",
"count": 1,
"items": [
""
]
},
{
"key": null,
"count": 2,
"items": []
}
]
```
All the expected groups and their sizes are here, but for the last one with `null` key there are no items returned.

If I make a direct request to database it returns me expected data.
```sql
select count(*), "Json"::jsonb -> 'c41c88f5-fb3b-4b77-bc11-2df17e6312b9' as "keys"
from "Entities"
group by "Json"::jsonb -> 'c41c88f5-fb3b-4b77-bc11-2df17e6312b9'
```
![image](https://github.com/dotnet/efcore/assets/5685857/641703c3-9ec4-4d9c-93b4-66b28aa1ce37)

### Provider and version information

EF Core version: **7.0.8**
Database provider: **Npgsql.EntityFrameworkCore.PostgreSQL**
Target framework: **.NET 6.0**
Operating system: **Any**
IDE: **Any**

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.