Linq方法不适用于实现IEnumerable的集合

时间:2018-07-19 10:56:16

标签: 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'不包含'Any'的定义,也找不到扩展方法'Any'接受类型为'DevExpress.XtraBars.BarItemLinkCollection'的第一个参数(是否缺少using指令或组装参考?)

我使用DevExpress 15.1.7。

问题是我想念的是什么。为什么我没有该属性的Linq方法可用?

1 个答案:

答案 0 :(得分:2)

我找不到版本15.1.7的任何文档,但是我假设返回了[Export(typeof(IPlugin))] public class CustomerPlugin : IPlugin { private readonly ICustomerService customerService; public CustomerPlugin (IServiceFactory serviceFactory) { customerService = serviceFactory.Get<Customer, CustomerService>(); } } ,因此您首先必须使用IEnumerableCast<T>()来获得{{1} }(请参阅https://stackoverflow.com/a/7757411/3936440)。

所以我认为你需要写

OfType<T>