Skip to content

Support ContainsKey (as an analyzer is telling us to use it) #26856

Description

@benmccallum

What problem are you trying to solve?
After recently upgrading to .NET 6, there's a new analyzer prompting you to convert dict.Keys.Contains("foo") to dict.ContainsKey("foo"), but that's not supported by EF Core, so a quick refactor can have nasty consequences.

Describe the solution you'd like
Can dict.ContainsKey be supported?

I could probably help out with some direction.

(didn't see any previous issues actually ask for this, sorry if it's been previously rejected)

Activity

  1. changed the title [-]Support ContainsKey[/-] [+]Support ContainsKey (as an analyzer is telling us to use it)[/+] on Nov 30, 2021
  2. benmccallum commented on Nov 30, 2021

    @benmccallum
    ContributorAuthor

    It might also be worth raising an issue on the analyzer side if they're able to selectively ignore this scenario (inside a Where on an IQueryable I guess?). I can also do that, would just need to figure out which repo to raise it on.

  3. roji commented on Nov 30, 2021

    @roji
    Member

    @benmccallum where in the EF Core API is ContainsKey not supported? Can you give a quick code sample that shows it?

  4. ajcvickers commented on Dec 1, 2021

    @ajcvickers
    Contributor

    @roji Do we translate ContainsKey?

  5. roji commented on Dec 1, 2021

    @roji
    Member

    @ajcvickers are we talking about property bags? Or some database type that's mapped to a .NET Dictionary (e.g. jsonb or hstore on PostgreSQL)? Just want to make sure we're talking about the same thing with @benmccallum

  6. stevendarby commented on Dec 1, 2021

    @stevendarby
    Contributor
    var vehicleTypes = new Dictionary<string, bool> { {"Car", true }, { "Lorry", true } };
    var vehicles1 = context.Set<Vehicle>().Where(x => vehicleTypes.Keys.Contains(x.Type)).ToList();
    var vehicles2 = context.Set<Vehicle>().Where(x => vehicleTypes.ContainsKey(x.Type)).ToList();

    Pretty sure it's about this sort of thing. First query translates to Type IN ('Car', 'Lorry') and second one can't be translated.

  7. roji commented on Dec 2, 2021

    @roji
    Member

    Triage decision: translating ContainsKey as above is something that is hard for EF Core to support in an efficient way. We will consider adding a diagnostics suppressor to suppress this warning when it appears inside an EF Core LINQ query (best effort). This would depend on having infrastructure for identifying EF Core LINQ queries at compile-time, which we don't currently have.

  8. added this to the Backlog milestone on Dec 2, 2021
  9. benmccallum commented on Dec 2, 2021

    @benmccallum
    ContributorAuthor

    Sorry I didn't get back in time and for my lack of an example. Just wanted to confirm the example that Steven shared is exactly it. It's the ContainsKey on a dictionary in a Where expression.

  10. JustArchi commented on May 24, 2022

    @JustArchi

    The issue seems to be pretty old but I've stumbled upon this just today as well. I'd have expected dictionary.ContainsKey(x) to be internally handled the same as dictionary.Keys.Contains(x). If this is not possible to do in an efficient way, CA1841 should take it into account when inside EF LINQ query.

  11. Rick-van-Dam commented on Oct 16, 2023

    @Rick-van-Dam

    The analyzer seem to be fixed in .net7 as it doesnt warn you anymore if you use Keys.Contains(x) in a ef query. On .net6 I still see the warning.

    Time to upgrade as I don't think the analyzer will be fixed in .net 6.

  12. kescherCode commented on Oct 16, 2023

    @kescherCode

    @Barsonax I encountered this in .NET 8-RC2 (along with EF Core 8), though.

  13. 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

No one assigned

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions