Search code examples
c#linqdevexpress

Linq methods not available for a collection implementing IEnumerable


I am trying to add an extension method for a Bar class.

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));
        ...
    }
}

Item link is property of a BarItemLinkCollection type. This class impelments IEnumerable.

But when I try to use any Linq method (e.g. Any), I got the error:

'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?)

I use DevExpress 15.1.7.

The question is what am I missing. Why I have no Linq methods available for the property?


Solution

  • I cannot find any documentation for the version 15.1.7 but I assume that IEnumerable is returned and therefore you first have to use Cast<T>() or OfType<T> to get an IEnumerable<T> (see https://stackoverflow.com/a/7757411/3936440).

    So, I think you need to write

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