我正在尝试为 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/