oRemove := List<string>{}
foreach oItem as KeyValuePair<string,int> in oDict
if oItem:Value == nValue
oRemove:Add( oItem:Key )
endif
next
foreach cKey as string in oRemove
oDict:Remove( cKey )
next
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
I need that in my ServiceContainer class: when a tab/window is closed, it should remove all registered services from the ServiceContainer object. Therefore I need to search for value and not for key.
And using LinQ for such a thing seems to be a little bit too much....
Wolfgang
Wolfgang Riedmann
Meran, South Tyrol, Italy
wolfgang@riedmann.it
https://www.riedmann.it - https://docs.xsharp.it
How big is this dictionary?.... unless it's got tens of thousands of entries I can't imagine it having the slightest impact on performance. I would expect the performance of this to be much better than looping through mulitple lists/dictionaries. Also you eliminate the Remove calls, which are probably the slowest part (I haven't got any evidence for that statement!).
I use this sort of thing all over the place without any issues at all, even in class properties to feed data into grid cells.
Contrary to what 'common myth' has to say (and the guys down the 'pub'), LINQ is just a standard part of current .NET coding.
Nothing special is required, and the namespaces are already included by default in the standard templates for Projects.
Nick and Robert have pointed you at some LINQ based solutions to your problem, some more eloquent than others (even if they do contain Lambda clauses). Robert's first one looks a bit neater and nicer, and is almost what Nick suggested.
We should be reaching for LINQ for all collection based processing - that's what it was designed for - COLLECTIONS.