Support ContainsKey (as an analyzer is telling us to use it)
See original GitHub issueWhat 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)
Issue Analytics
- State:
- Created 2 years ago
- Reactions:3
- Comments:8 (7 by maintainers)
Top Results From Across the Web
Dictionary<TKey,TValue>.ContainsKey(TKey) Method
It also shows how to use the TryGetValue method to retrieve values, which is an efficient way to retrieve values when a program...
Read more >How can the containsKey() method of a Java Hash Table ...
A string that you put into a hashtable isn't stored at the ... So you see that an array can find things fast...
Read more >Why does java.util.Map.containsKey throw a null pointer ...
1. You use containsKey on a map that supports a null key. As the user of this function you expect to get either...
Read more >PVS-Studio evolution: data flow analysis for related variables
Our clients write us about their issues — we do everything to make PVS-Studio better. And such articles help us to tell the...
Read more >HashMap containsKey() Method in Java
It takes the key element as a parameter and returns True if that element is mapped in the map. Syntax: Hash_Map.containsKey(key_element).
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.
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,CA1841
should take it into account when inside EF LINQ query.