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?
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.
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);