Amir
Amir

Reputation: 1979

sort list of Controls from a custom class type

I have a problem in order my list, I check most of the example but I think my problem is unique. Please check and help me to get it done.

List<Control> lc = new List<Control>();

foreach (Control control in fLP.Controls)
{
   Dashboard_Env_Details ded =  (Dashboard_Env_Details)control;
   ded.refreshUC();
   lc.Add(control);
}

List<Control> Sortedlc = lc.OrderBy((o => ((Dashboard_Env_Details)o).custTask.getNoInvalidObjects).ToList();

I am receiving below error:

Severity Code Description Project File Line Suppression State Error CS0411 The type arguments for method 'Enumerable.OrderBy(IEnumerable, Func)' cannot be inferred from the usage. Try specifying the type arguments explicitly. KeepMyServerInfo C:\Users\CM_Dashboard.cs 141 Active

Upvotes: 0

Views: 115

Answers (1)

Tim Schmelter
Tim Schmelter

Reputation: 460168

You have too many left parentheses. I would use OfType or Cast or store the controls in a List<Dashboard_Env_Details> instead:

List<Control> Sortedlc = lc.Cast<Dashboard_Env_Details>()
 .OrderBy(o => o.custTask.getNoInvalidObjects)
 .Select(o => (Control) o)
 .ToList();

If the list contains also other controls you could use OfType.

Upvotes: 4

Related Questions