C#中Chart中的Annotation对象派生自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);

它有一个方法.Containes(),但我不能将Linq与它一起使用.

如何在不使用for或foreach循环的情况下,使用Linq或类似的方法判断图表中的注释是否包含特定的注释名称

推荐答案

documentationFindByName方法.

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

var annotaion = chart.Annotations.FindByName(annotationNameToFind);
bool annotationExists = annotaion is not null;

此外,您还可以使用LINQ迭代它们

bool annotationExists = chart.Annotations.GetEnumerator().Any(annotation => annotation.Name == annotationNameToFind);

Csharp相关问答推荐

在C# 11之前, struct 中的每个字段都必须显式分配?不能繁殖

自定义JsonEditor,用于将SON序列化为抽象类

注册通用工厂的C# Dep注入

程序集.加载从exe的异常

如何在C#中删除一个特殊字符,如"使用Regex"

为什么我的ASP.NET核心MVC应用程序要为HTML元素添加一些标识符?

将XPS转换为PDF C#

try 在Blazor项目中生成html

Nuget包Serilog.Sinks.AwsCloudwatch引发TypeLoadExceptions,因为父类型是密封的

Blazor EventCallback<;MyType<;T>;>;

C#XmlSerializer-输出控制新行的多个XML片段

为什么C#认为这个非托管 struct 有一个重叠

如何在Cosmos SDK中控制超时、重试和重试之间的延迟?

Savagger使用Fastendpoint更改用户界面参数

在C#中,非静态接口方法的抽象和虚拟是冗余的吗?

.NET 8 DI GetServices<;对象&>不工作

委托RequestDelegate不带2个参数-ASP.NET Core 8最小API

GODOT 4向C#中的字符串参数发送信号以等待

通过mini kube中的远程调试Pod与从emoteProcessPickerScript中解析错误输出的代码错误进行比较

如何使用moq和xUnit对删除操作进行单元测试?