我們的項目目前使用2種方法在頁面內查找控件。 第一種是遞歸使用。FindControl。 另一種是使用LINQ這樣的:ASP.Net中哪個更快?
(from n in Page.Controls.Cast<Control>().Descendants(c => c.Controls.Cast<Control>())
where (n as Label != null && n.ID == "TaskIDLabel")
select n).First() as Label;
使用這個擴展:
static public IEnumerable<T> Descendants<T>(this IEnumerable<T> source,
Func<T, IEnumerable<T>> DescendBy)
{
foreach (T value in source)
{
yield return value;
foreach (T child in DescendBy(value).Descendants<T>(DescendBy))
{
yield return child;
}
}
}
其中的這2種方法比較好?哪個更快?
因爲一個建立在另一個之上,所以它們都不會更好。 – 2011-04-04 16:24:27
測試並找出答案。 – Jess 2011-04-04 16:28:14
@Justin - 我認爲OP在詢問'FindControl'與問題中發佈的代碼相比。 – Greg 2011-04-04 16:38:00