1

I want to do this

return array.Any(IsOdd);

but instead of an array i have a list of objects where IsOdd takes in the object.Number property as its argument.

I tried this

return objectList.Select(x => x.Number).Any(IsOdd);

but got this error "'Select' is not a member of 'System.Collections.Generic.List(Of myObject)'."

Also my code is actually in VB and I'm using vs 2010 but targeting .net 2.0.

Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
Ian Hern
  • 641
  • 1
  • 8
  • 16

2 Answers2

4

Updated answer

There is no official way to use LINQ in .NET 2.0, although if you are only interested in LINQ to Objects there's LINQBridge to help.

Original answer

First off, you can simply use the other overload of Any and write

return objectList.Any(o => IsOdd(o.Number));

And second, this sounds like you have forgot using System.Linq in your file -- although in that case, straight calling Any would not work either.

Community
  • 1
  • 1
Jon
  • 428,835
  • 81
  • 738
  • 806
1

Since you're targeting 2.0, the quick answer is that .NET 2.0 does not support LINQ or extension methods.

If you decide you don't want to use LINQBridge as @Jon suggests, you can very easily recreate your own Any method as follows:

public static class MyLINQBridge {
    public delegate TResult Func<T1, TResult>(T1 first);
    public static bool Any<T>(this IEnumerable<T> source, Func<T, bool> predicate) {
        foreach (var item in source) {
            if (predicate(item)) {
                return true;
            }
        }
        return false;
    }

}

Since you're using VS2010, the compiler supports lambda methods and can support extension methods, so your final code would look like this:

return objectList.Any(o => IsOdd(o.Number));
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85