I have the following function to get all the child controls of type.
private IEnumerable<Control> GetControlsOfType(Control control, Type type)
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControlsOfType(ctrl, type))
.Concat(controls)
.Where(c => c.GetType() == type);
}
I am trying to convert it using generic type parameter.
private IEnumerable<T> GetControlsOfType<T>(Control control) where T: Control
{
var controls = control.Controls.Cast<Control>();
return controls.SelectMany(ctrl => GetControlsOfType<T>(ctrl))
.Concat(controls)
.Where(c => c.GetType() == T);
}
The code has the error on .Concat.
Error 6 ‘System.Collections.Generic.IEnumerable
<T>’ does not contain a
definition for ‘Concat’ and the best extension method overload
‘System.Linq.Queryable.Concat<TSource>(System.Linq.IQueryable<TSource>,
System.Collections.Generic.IEnumerable<TSource>)’ has some invalid
arguments
How to fix the issue?
Try adding
.OfType<Control>()afterGetControlsOfType<T>(ctrl)and instead of yourWhereSo your code reads: