EF selects wrong data (GroupBy)
- Dominant language
- C#
- Stars
- 14.8k
- Forks
- 3.4k
- PR merge metrics
- PR metrics pending
Description
### Bug description
Ef selects wrong data. I noticed it as it crashed at `.ToDictionaryAsync` using a key I grouped before. Error message "An item with the same key has already been added"
### Your code
```csharp
USE [Test]
GO
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Foo](
[id] [int] NOT NULL,
[name] [nvarchar](255) NOT NULL,
[employeeId] [int] NOT NULL,
[reviewerId] [int],
CONSTRAINT [PK_Foo] PRIMARY KEY CLUSTERED
(
[id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
insert into dbo.Foo (id, name, [employeeId], [reviewerId]) values (1, 'Row1', 1, 0);
insert into dbo.Foo (id, name, [employeeId], [reviewerId]) values (5, 'Row2', 1, null);
Selecting the data with
IDictionary metaInfos = await db.Foos
.GroupBy(pt => pt.EmployeeId)
.Select(g => new Data()
{
EmployeeId = g.Key,
ReviewerIds = g.Select(pt => pt.ReviewerId).Distinct().ToList(),
})
.ToDictionaryAsync(mi => mi.EmployeeId.ToString());
class Data()
{
public int EmployeeId { get; set; }
public List ReviewerIds { get; set; }
}
```
### Stack traces
```text
ArgumentException: An item with the same key has already been added. Key: 1
```
### Verbose output
```text
```
### EF Core version
8.0.8
### Database provider
Sql Server
### Target framework
.NET 8
### Operating system
Windows 10
### IDE
_No response_
Contributor guide
Assessment
This issue has not been assessed yet.