【发布时间】:2016-03-10 00:37:08
【问题描述】:
我将这本字典 mappings 声明为 Dictionary<string, HashSet<string>>。
我也有这种方法可以对字典中的哈希集进行处理:
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
}
在一种情况下,我需要对映射字典中的多个键执行此操作,我认为最好重写和过滤键列表,而不是为每个键调用 DoStuff 以优化执行。
目前我这样做:
DoStuff("key1", 123);
DoStuff("key2", 123);
DoStuff("key4", 123);
DoStuff("key7", 123);
DoStuff("key11", 123);
逻辑上是这样的,而不是为每个调用 DoStuff(FilterOnKeys 不是一种方法 - 正是我想要的......):
foreach (var classEntry in
from c in mappings.FilterOnKeys("key1", "key2", "key4", "key7", "key11")
where c.StartsWith(iClassId + "(")
select c)
{
DoStuffWithEntry(classEntry);
}
【问题讨论】:
标签: c# linq optimization