我有一个通用方法,它使用一系列有效的 foreach 循环将页面上的所有 html 控件添加到通用列表中。是否可以将其转换为 LINQ 表达式?
private List<T> GetControls<T>() where T : HtmlControl
{
List<T> c = new List<T>();
foreach (HtmlControl c1 in Controls)
{
foreach (HtmlControl c2 in c1.Controls)
{
if (c2.GetType() == typeof(HtmlForm))
{
foreach (Control c3 in c2.Controls)
{
if (c3.GetType() == typeof(ContentPlaceHolder))
{
foreach (HtmlControl c4 in c3.Controls)
{
if (c4.GetType() == typeof(T))
{
c.Add((T)c4);
}
if (c4.GetType() == typeof(PlaceHolder))
{
foreach (HtmlControl c5 in c4.Controls)
{
if (c5.GetType() == typeof(T))
{
c.Add((T)c5);
}
}
}
}
}
}
}
}
}
return c;
}