2

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?

Piotrek
  • 169
  • 3
  • 17
  • Post the `using ...;` block of lines from the top of your file. – bommelding Jul 19 '18 at 10:59
  • As @bommelding already mentioned, there must be something wrong with the usings. Your code compiles just fine on my machine. – ViRuSTriNiTy Jul 19 '18 at 11:11
  • @bommelding - done. – Piotrek Jul 19 '18 at 11:19
  • Still compiles on my machine. Which DevExpress version do you use? – ViRuSTriNiTy Jul 19 '18 at 12:03
  • @ViRuSTriNiTy The DevExpress version added - 15.1.7 – Piotrek Jul 19 '18 at 12:22
  • 1
    I cannot find any documentation for this specific version but i assume that `IEnumerable` is returned and therefore you first have to use `Cast()` or `OfType` to get an `IEnumerable` (see https://stackoverflow.com/a/7757411/3936440). – ViRuSTriNiTy Jul 19 '18 at 12:46
  • 1
    Does something like this work? `(bar.ItemLinks as IEnumerable).Any(x => x.Item.Tag.Equals(tag));` – Andrew K Jul 19 '18 at 13:05
  • @ViRuSTriNiTy - Yeap, this worked. What I don't understand now is why my initial code compiles on your site. Does it mean that in your version of DevExpress IEnumerable has been added to interfaces implemented by BarItemLinkCollection? – Piotrek Jul 20 '18 at 07:58
  • Yep, i have DevExpress 18 and in this version `BarItemLinkCollection` implements `IEnumerable`. I posted an answer. – ViRuSTriNiTy Jul 20 '18 at 08:30

1 Answers1

3

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

ViRuSTriNiTy
  • 5,017
  • 2
  • 32
  • 58