Skip to content

Hashset contains becomes a traditional IN statement instead of the new OpenJSON parameter #32365

Description

@ngelotte

File a bug

If you use a HashSet for the parameter which is used by contains it generates the old style non parameterized query

HashSet<string> filterData = ["Thing1", "Thing2"];
var results =  db.TableOfStuff.Where(t => filterData.Contains(t.Item)).ToList();

The following code generates the new parameterized query but it seems like an unnecessary ToList and an easy place to miss adding the ToList and end up with a performance degradation.

HashSet<string> filterData = ["Thing1", "Thing2"];
var results =  db.TableOfStuff.Where(t => filterData.ToList().Contains(t.Item)).ToList();

Include provider and version information

EF Core version: 8.0
Database provider: Microsoft.EntityFrameworkCore.SqlServer
Target framework: .Net 8.0
Operating system: Windows
IDE: Visual Studio 17.8

Activity

  1. self-assigned this
    on Nov 20, 2023
  2. AlexeiScherbakov commented on Nov 20, 2023

    @AlexeiScherbakov
  3. roji commented on Nov 20, 2023

    @roji
  4. AlexeiScherbakov commented on Nov 21, 2023

    @AlexeiScherbakov
  5. roji commented on Nov 21, 2023

    @roji
    Member

    Thanks for reporting this, I've confirmed the bug and will investigate.

    Repro
    await using var ctx = new BlogContext();
    await ctx.Database.EnsureDeletedAsync();
    await ctx.Database.EnsureCreatedAsync();
    
    var ids = new HashSet<int> { 1, 2 };
    _ = ctx.Blogs.Where(x => ids.Contains(x.Id)).ToList();
    
    public class BlogContext : DbContext
    {
        public DbSet<Blog> Blogs { get; set; }
    
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
            => optionsBuilder
                .UseSqlServer(@"Server=localhost;Database=test;User=SA;Password=Abcd5678;Connect Timeout=60;ConnectRetryCount=0;Encrypt=false")
                .LogTo(Console.WriteLine, LogLevel.Information)
                .EnableSensitiveDataLogging();
    }
    
    public class Blog
    {
        public int Id { get; set; }
        public string? Name { get; set; }
    }
  6. dggmez commented on Nov 22, 2023

    @dggmez

    Came here to report this. It seems it works correctly with List, IEnumerable and arrays. It doesn't work with HashSet, ImmutableList, ImmutableArray, ImmutableHashSet, FrozenSet, etc.

  7. added this to the Backlog milestone on Nov 30, 2023
  8. ibrahim33 commented on Jan 11, 2024

    @ibrahim33

    Hello everyone. Is there an ETA when this will be released? We are currently using a custom implementation of OPENJSON and we cannot remove it nor upgrade to EF 8 because of the bug.

    LINQ with HashSet:

    var productSets= await dbContext.ProductSets()
                .Where(ps => ids.ToHashSet().Contains(ps.ProductId))
                .ToListAsync(cancellationToken)

    QUERY with HashSet:

    SELECT [p].[Id], [p].[ProductId], [p].[ReactivateIfSubProductsReappear], [p].[Version], [p0].[Id], [p0].[IsMainProduct], [p0].[ProductId], [p0].[ProductSetId], [p0].[Quantity]
    FROM [externaldata].[ProductSet] AS [p]
    LEFT JOIN [externaldata].[ProductSetProduct] AS [p0] ON [p].[Id] = [p0].[ProductSetId]
    WHERE [p].[ProductId] IN (CAST(213171 AS bigint), CAST(234107 AS bigint), CAST(254039 AS bigint), CAST(252018 AS bigint), CAST(257030 AS bigint), CAST(282759 AS bigint), CAST(290215 AS bigint), CAST(352284 AS bigint), CAST(354318 AS bigint), CAST(354179 AS bigint))
    ORDER BY [p].[Id]

    LINQ with List:

    var productSets= await dbContext.ProductSets()
                .Where(ps => ids.ToList().Contains(ps.ProductId))
                .ToListAsync(cancellationToken)

    QUERY with List:

    SELECT [p].[Id], [p].[ProductId], [p].[ReactivateIfSubProductsReappear], [p].[Version], [p0].[Id], [p0].[IsMainProduct], [p0].[ProductId], [p0].[ProductSetId], [p0].[Quantity]
    FROM [externaldata].[ProductSet] AS [p]
    LEFT JOIN [externaldata].[ProductSetProduct] AS [p0] ON [p].[Id] = [p0].[ProductSetId]
    WHERE [p].[ProductId] IN (
        SELECT [i].[value]
        FROM OPENJSON(@__ids_0) WITH ([value] bigint '$') AS [i]
    )
    ORDER BY [p].[Id]
  9. 10 remaining items

  10. modified the milestones: Backlog, 9.0.0 on May 29, 2024
  11. cincuranet commented on Jun 4, 2024

    @cincuranet
    Contributor

    Note to self (from design): We should handle Contains on any "ICollection", not only HashSet.

  12. OndrejUzovic commented on Jun 10, 2024

    @OndrejUzovic

    Since by fixing this bug the workaround for #32147 will not work anymore.
    Are there any plans to fix also #32147?

  13. roji commented on Jun 10, 2024

    @roji
    Member

    @OndrejUzovic yeah, we're definitely planning on fixing that (or providing a better workaround) for 9.0.

  14. modified the milestones: 9.0.0, on Jun 21, 2024
  15. modified the milestones: , 9.0.0 on Oct 12, 2024
  16. added theissue type on Jun 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions