我有一個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。