You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Support ContainsKey (as an analyzer is telling us to use it) #26856
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)
Reacted by Edward Brey, Benoit Gerard and Alex Goldobin
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.
@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
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.
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.
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.
Activity
[-]Support ContainsKey[/-][+]Support ContainsKey (as an analyzer is telling us to use it)[/+]benmccallum commentedon Nov 30, 2021
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.
roji commentedon Nov 30, 2021
@benmccallum where in the EF Core API is ContainsKey not supported? Can you give a quick code sample that shows it?
ajcvickers commentedon Dec 1, 2021
@roji Do we translate
ContainsKey?roji commentedon Dec 1, 2021
@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
stevendarby commentedon Dec 1, 2021
Pretty sure it's about this sort of thing. First query translates to
Type IN ('Car', 'Lorry')and second one can't be translated.roji commentedon Dec 2, 2021
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.
benmccallum commentedon Dec 2, 2021
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.
JustArchi commentedon May 24, 2022
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 asdictionary.Keys.Contains(x). If this is not possible to do in an efficient way,CA1841should take it into account when inside EF LINQ query.Rick-van-Dam commentedon Oct 16, 2023
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.
kescherCode commentedon Oct 16, 2023
@Barsonax I encountered this in .NET 8-RC2 (along with EF Core 8), though.