Our project is currently using 2 ways to find a Control inside of pages. The first is to use .FindControl recursively. The other is to use LINQ like this:
(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;
Which uses this Extension:
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;
}
}
}
Which of these 2 methods is better? Which is faster?