嗨我有一個通用的方法,它將我在頁面上的所有html控件添加到使用一系列foreach循環工作的通用列表中,是否可以將其轉換爲linq表達式?將這一系列的foreach循環轉換爲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;
}
謝謝
的[遊客](http://www.oodesign.com/visitor-pattern.html)設計模式,將有助於解開代碼喜歡這個。 – Jon
乾杯喬恩,應該看看它。 –