I have this dictionary mappings declared as a Dictionary<string, HashSet<string>>.
I also have this method to do stuff on a hashset in the dictionary:
public void DoStuff(string key, int iClassId){
foreach (var classEntry in
from c in mappings[key]
where c.StartsWith(iClassId + "(")
select c)
{
DoStuffWithEntry(classEntry);
}
}
private void DoStuffWithEntry(string classEntry){
// Do stuff with classEntry here
}
In one case, I need to do this on a number of keys in the mappings dictionary, and I was thinking it was better to rewrite and filter on a list of keys instead of calling DoStuff for each key to optimise the execution.
Currently I do this:
DoStuff("key1", 123);
DoStuff("key2", 123);
DoStuff("key4", 123);
DoStuff("key7", 123);
DoStuff("key11", 123);
Logically something like this instead of calling DoStuff for each (FilterOnKeys is not a method – just what I want…):
foreach (var classEntry in
from c in mappings.FilterOnKeys("key1", "key2", "key4", "key7", "key11")
where c.StartsWith(iClassId + "(")
select c)
{
DoStuffWithEntry(classEntry);
}
It sounds like you want:
(I would personally use a separate variable for the query just for readability – I’m not too keen on the declaration bit of a
foreachloop getting huge.)