C# 中 Chart 中的 Annotations 对象派生自 Collection<>,但由于某种原因我无法在其上使用 .Any() 或 .Find() 方法。
所以这不起作用:
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);
它有一个方法 .Contains(),但我不能将 Linq 与它一起使用。
如何在不使用 for 或 foreach 循环的情况下使用 Linq 或类似名称检查图表中的注释是否包含特定注释
查看文档有FindByName方法。
返回 Annotation 对象,如果该对象不存在则返回 null
var annotaion = chart.Annotations.FindByName(annotationNameToFind);
bool annotationExists = annotaion is not null;