c# - Linq 方法不可用于实现 IEnumerable 的集合

标签 c# linq devexpress

我正在尝试为 Bar 添加扩展方法类。

using System;
using System.ComponentModel;
using DevExpress.XtraBars;
using System.Drawing;
using System.Windows.Forms;
using System.Linq;
using System.Data;

public static class BarExtensions
{
    public static BarItemLink GetBarItemLinkByTag(this Bar bar, object tag)
    {
        BarItemLink foundItemLink = null;
        bool a = bar.ItemLinks.Any(x => x.Item.Tag.Equals(tag));
        ...
    }
}

项目链接是 BarItemLinkCollection 的属性类型。该类实现IEnumerable

但是当我尝试使用任何 Linq 方法(例如 Any)时,我收到错误:

'DevExpress.XtraBars.BarItemLinkCollection' does not contain a definition for 'Any' and no extension method 'Any' accepting a first argument of type 'DevExpress.XtraBars.BarItemLinkCollection' could be found (are you missing a using directive or an assembly reference?)

我使用 DevExpress 15.1.7。

问题是我错过了什么。为什么我没有可用于该属性的 Linq 方法?

最佳答案

我找不到版本 15.1.7 的任何文档,但我假设 IEnumerable返回,因此您首先必须使用 Cast<T>()OfType<T>获得 IEnumerable<T> (参见https://stackoverflow.com/a/7757411/3936440)。

所以,我认为你需要写

bool a = bar.ItemLinks.Cast<BarItemLink>().Any(x => x.Item.Tag.Equals(tag))

关于c# - Linq 方法不可用于实现 IEnumerable 的集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51420862/

相关文章:

c# - 我是疯了还是 Math.Pow 坏了?

c# - linq查询返回唯一项的嵌套列表

c# - 在新进程中运行时窗口标题中的 "Not Responding"

linux - 如何使用netcore在Linux centos 7中加载DevExpress报告

C# OleDb INSERT INTO 溢出异常

c# - 基于Array Results的LINQ SUM数据

c# - 如何将 C/C++ dll 导入到 .NET 应用程序

C# 对父/子列表进行排序以产生平面输出

c# - 将 LINQ 与 Open XML 结合使用 (Excel)

c# - 对集合的一部分执行接口(interface)方法时,项目从 Ienumerable 列表中消失