Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Get an exception

At least one object must implement IComparable

plotter.Children.OfType<linegraph>().Where(x => x.Description.ToString() == (sender as CheckBox).Content.ToString()).Max();

What I have tried:

var lineToRemove = ((sender as CheckBox).ToolTip == null) ?
plotter.Children.OfType<linegraph>().Where(x => x.Description.ToString() == (sender as CheckBox).Content.ToString()).Max() :
plotter.Children.OfType<linegraph>().Where(x => x.Description.ToString() == (sender as CheckBox).Content.ToString() + " | " + (sender as CheckBox).ToolTip.ToString()).Max();


if (lineToRemove != null)
{
int a = plotter.Children.IndexOf(lineToRemove);
plotter.Children.Remove(lineToRemove);
plotter.Children.RemoveAt(a++);
}
Posted
Updated 11-Oct-19 7:55am

Check your linegraph type, and also make very sure you have the right line.

You get that error when trying to order a collection of objects that doesn't support IComparable, normally when you try to OrderBy them - I've not seen it for Max, but I guess it may be possible.
Certainly, when I try to get close to your code (and without your actuall classes I can't get that close) I get no problems:
C#
private class MyClass {
    public string Desc { get; set; }
    public MyClass(string s) { Desc = s; }
    }
private void FrmMain_Shown(object sender, EventArgs e)
    {
    List<MyClass> items = new List<MyClass>() { new MyClass("one"), new MyClass("two"), new MyClass("three") };
    MyClass y = items.Where(x => x.Desc.ToString() == "two".ToString()).Max();
    }
 
Share this answer
 
Comments
Richard Deeming 11-Oct-19 13:48pm    
That's an odd behaviour of LINQ - your code works because the input sequence contains precisely one item. If the input sequence contains more than one item, you'll get the OP's exception.

It's understandable, because Max needs to compare the items in order to work out which one is "biggest". If the type doesn't implement IComparable, there's no way to do that. :)
There are multiple matching items in the sequence, and your linegraph type doesn't implement IComparable, so there is no way for the Max method to know which item is "biggest".

Perhaps you meant to use the FirstOrDefault method instead?
C#
var lineToRemove = ((sender as CheckBox).ToolTip == null) 
    ? plotter.Children.OfType<linegraph>().FirstOrDefault(x => x.Description.ToString() == (sender as CheckBox).Content.ToString()) 
    : plotter.Children.OfType<linegraph>().FirstOrDefault(x => x.Description.ToString() == (sender as CheckBox).Content.ToString() + " | " + (sender as CheckBox).ToolTip.ToString());
Also note that sender as CheckBox can return null if the sender is not actually a CheckBox. That will lead to a NullReferenceException. If you're certain that the sender is always a CheckBox, use a cast instead.
C#
var checkBox = (CheckBox)sender;

string valueToMatch = checkBox.ToolTip is null
    ? checkBox.Content.ToString()
    : string.Format("{0} | {1}", checkBox.Content, checkBox.ToolTip);

var lineToRemove = plotter.Children.OfType<linegraph>().FirstOrDefault(x => x.Description == valueToMatch);
 
Share this answer
 
Comments
Member 12237141 14-Oct-19 0:47am    
THANKS SIR.... It Works for me.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900