Where()与Dictionary.Where中的Replace()(p => p.Key为T)

aba*_*hev 2 c# linq asp.net dictionary web-controls

我有一个System.Collections.Generic.Dictionary<System.Web.UI.Control, object>所有键可以是类型System.Web.UI.WebControls.HyperLink或类型的地方System.Web.UI.WebControls.Label.

我想改变Text每个控件的属性.因为HyperLink没有实现(为什么?!)ITextControl,我需要显式地转换Label或HyperLink:

Dictionary<Control,object> dic = ..

dic
  .Where(p => p.Key is HyperLink)
  .ForEach(c => ((HyperLink)c).Text = "something")

dic
  .Where(p => p.Key is Label)
  .ForEach(c => ((Label)c).Text = "something")
Run Code Online (Sandbox Code Playgroud)

有办法解决这种方法吗?

dtb*_*dtb 5

略微优雅,但保留问题:

foreach (HyperLink c in dic.Keys.OfType<HyperLink>())
{
    c.Text = "something";
}

foreach (Label c in dic.Keys.OfType<Label>())
{
    c.Text = "something";
}
Run Code Online (Sandbox Code Playgroud)