Search code examples
c#asp.netlinqdictionaryweb-controls

Where() with Replace() in Dictionary.Where(p => p.Key is T)


I have a System.Collections.Generic.Dictionary<System.Web.UI.Control, object> where all keys can be either type of System.Web.UI.WebControls.HyperLink or type of System.Web.UI.WebControls.Label.

I want to change Text property of each control. Because HyperLink doesn't implement (why?!) ITextControl, I need to cast Label or HyperLink explicitly:

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")

Are there ways to workaround such approach?


Solution

  • You could create a class that derives from HyperLink and let your class inherit from ITextControl. Should be clear where to go from there...