dotnet / dotnet/efcore

Using (i.e. filtering) projected primitive collection down the line fail

Open
#35,580 0 comments 0 reactions 0 assignees View on GitHub
area-primitive-collections area-query
Dominant language
C#
Stars
14.8k
Forks
3.4k
PR merge metrics
PR metrics pending

Description

This code fails.

```csharp
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Diagnostics;

using (var db = new MyContext())
{
var listOfInt = new List { 1 };
await db.Items.Select((e) => new { e.Id, listOfInt }).Where(x => x.listOfInt.Any()).ToListAsync();
}

class MyContext : DbContext
{
public DbSet Items { get; set; }

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder
.UseSqlite("Data Source=triage.db")
.LogTo(Console.WriteLine, filter: (id, level) => id == RelationalEventId.CommandExecuting)
.EnableSensitiveDataLogging();
}
}
class Item
{
public int Id { get; set; }
public string? Name { get; set; }
}
```

```text
System.InvalidOperationException: The LINQ expression 'DbSet()
.Where(i => __listOfInt_0
.Any())' could not be translated. Additional information: Translation of method 'System.Linq.Enumerable.Any' failed. If this method can be mapped to your custom function, see https://go.microsoft.com/fwlink/?linkid=2132413 for more information. Either rewrite the query in a form that can be translated, or switch to client evaluation explicitly by inserting a call to 'AsEnumerable', 'AsAsyncEnumerable', 'ToList', or 'ToListAsync'. See https://go.microsoft.com/fwlink/?linkid=2101038 for more information.
```

Related to #35579.

Contributor guide

Open the contributing guide

Research direction

Run the provided SQLite reproduction with EF Core and inspect the failing projection/filter query and its translation error. Read the related issue #35579 for context; the issue does not specify the expected behavior or a named test, so confirm the intended outcome before defining completion.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, sqlite
Domain
database
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.