-
Notifications
You must be signed in to change notification settings - Fork 3.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Combination of GroupBy
, FirstOrDefault
and Select
throws a KeyNotFoundException
#30052
Comments
Hello. I'll just add that I get the same error doing pretty much the exact same thing. Slight difference is that I'm using SqlServer and LocalDB, targeting .NET 6, and using EF Core 7.0.5 with code-first migrations. I made a small runnable sample project that exhibits the error, too. .csproj <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net6.0</TargetFramework>
<RootNamespace>EF7_group_by_select</RootNamespace>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="7.0.5">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="7.0.5" />
</ItemGroup>
<ItemGroup>
<Folder Include="Migrations\" />
</ItemGroup>
</Project> Program.cs using Microsoft.EntityFrameworkCore;
namespace EF7_group_by_select
{
public class Program
{
static async Task Main()
{
var dbContext = new MyContext();
var filter = new[] { "foo", "bar" };
var listOfIds = await dbContext.Items
.Where(x => filter.Contains(x.RefId))
.GroupBy(x => x.RefId)
.Select(grp => grp.OrderByDescending(x => x.Date).First())
.Select(x => x.Id)
.ToListAsync();
Console.WriteLine(string.Join(Environment.NewLine, listOfIds));
}
}
public class MyContext : DbContext
{
public DbSet<Item> Items { get; set; }
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlServer("Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=example-db;Integrated Security=True");
}
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
modelBuilder.Entity<Item>().HasData(
new Item { Id = "a1", Date = new(2023, 6, 12), RefId = "foo" },
new Item { Id = "b2", Date = new(2023, 5, 15), RefId = "bar" },
new Item { Id = "c3", Date = new(2023, 1, 10), RefId = "baz" },
new Item { Id = "d4", Date = new(2023, 2, 25), RefId = "foo" },
new Item { Id = "e5", Date = new(2023, 3, 18), RefId = "bar" });
}
}
public class Item
{
public string Id { get; set; }
public DateTime Date { get; set; }
public string RefId { get; set; }
}
} Exception
|
Not sure if it's relevant but I wanted to mention that this worked in the old EF6. Just noticed this error coming up after migration to EF Core. |
yeah i've same issue when i need AsQueryable. here the code:
|
If you use
GroupBy
,Select
that gets an row from the group withFirstOrDefault
and then try to reduce the amount of columns returned with anotherSelect
, you get an KeyNotFoundException:Full code
.csproj
Program.cs
Expected output
Notes
Changing the query to
First
without the null-check also throws an exception:Changing the query to start from
Reports
(withoutGroupBy
) and then get the largest one, the query works as expected:If I add an
AsAsyncEnumerable
before the select, the query works as intended:Stack traces
Include provider and version information
EF Core version: 7.0.2
Database provider: bug found in
Microsoft.EntityFrameworkCore.SqlServer
reproduced inMicrosoft.EntityFrameworkCore.Sqlite
Target framework: .NET 7.0 (SDK: 7.0.200,
27f0a7fa5a
)Operating system: Windows 11 (22H2)
IDE: Rider 2022.3.1
The text was updated successfully, but these errors were encountered: