如果图表的注释包含使用 Linq 的特定名称的注释,如何在 C# 中检查

问题描述 投票:0回答:1

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 或类似名称检查图表中的注释是否包含特定注释

c# winforms charts mschart
1个回答
0
投票

查看文档FindByName方法。

返回 Annotation 对象,如果该对象不存在则返回 null

var annotaion = chart.Annotations.FindByName(annotationNameToFind);
bool annotationExists = annotaion is not null;
最新问题
© www.soinside.com 2019 - 2024. All rights reserved.