我有一个Dictionary<string, bool>
,其中关键 - 控件的ID和价值 - 它是可见的状态设置:避免在LINQ查询双控搜索
var dic = new Dictionary<string, bool>
{
{ "rowFoo", true},
{ "rowBar", false },
...
};
一些控件可以null
,即dic.ToDictionary(k => this.FindControl(k), v => v)
不起作用,因为键还可以不能为空。
我可以做下一个:
dic
.Where(p => this.FindControl(p.Key) != null)
.ForEach(p => this.FindControl(p.Key).Visible = p.Value); // my own extension method
,但是这将调用FindControl()
两次,每次关键。
如何避免重复搜索并只选择那些适合控制的键?
喜欢的东西:
var c= FindControl(p.Key);
if (c!= null)
return c;
但使用LINQ。