Reputation: 3594
The Annotations object in Chart in C# is derived from a Collection<>, but I cannot use .Any() or .Find() methods on it for some reason.
So this does not work:
using System.Windows.Forms.DataVisualization.Charting;
Chart chart = new Chart();
// Your annotation name to search for
string annotationNameToFind = "YourAnnotationName";
// Check if the annotation exists with the given name
bool annotationExists = chart.Annotations.Any(annotation => annotation.Name == annotationNameToFind);
It has a method .Contains(), but I cannot use Linq with it.
How do I check if the annotations in a chart contain a specific annotation by name with Linq or something similar without using a for or foreach loop
Upvotes: 1
Views: 134
Reputation: 2820
Look at documentation there is FindByName Method.
Returns An Annotation object, or null if the object does not exist
var annotaion = chart.Annotations.FindByName(annotationNameToFind);
bool annotationExists = annotaion is not null;
Also, you can iterate over them With LINQ
bool annotationExists = chart.Annotations.GetEnumerator().Any(annotation => annotation.Name == annotationNameToFind);
Upvotes: 0