Skip to content

ICollection.Contains(...) on Json Column defined as a ICollection doesn't produce JSON_CONTAINS #1974

Open
@idavidmarshali

Description

@idavidmarshali

Consider this entite:

public class House{
     [Key]
     public int Id {get;set;}
}

public class Humans {
      [Key]
     public int Id {get;set;}
     [ColumnType(TypeName="json")]
     public List<int> HouseIds {get;set;} = [];
}

When we try to project this into another object like so:

context.Houses.Select(h => new {
    Count = context.Humans.Count(e => e.HouseIds.Contains(h.Id))
    Id = h.Id
} );

I Get the following error:

[System.InvalidOperationException: The LINQ expression 'DbSet<Leads>()
    .Count(l => EF.Property<List<int>>(l, "HouseIds")
        .AsQueryable()
        .Contains(id))' could not be translated. 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'.

Am i doing something wrong or the provider doesn't create a JSON_CONTAINS() query for List.Contains()?

because if i rewrite the query to:

context.Houses.Select(h => new {
    Count = context.Humans.Count(e => EF.Functions.JsonContains(e.HouseIds, h.Id))
    Id = h.Id
} );

it works fine and generates a correct SQL query.

Further Information:

  • Using Mariadb 10
  • Pomelo.EntityFrameworkCore.MySql Version="8.0.2"
  • Pomelo.EntityFrameworkCore.MySql.Json.Microsoft Version="8.0.2"
  • Microsoft.EntityFrameworkCore Version="8.0.2"

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions