tags:

views:

150

answers:

6

Hi, just wondering if there is any way of checking if Value A is equal to ANY value within an array (without using large loop functions) - sort of like a "Where" function.

e.g.

if (DataRow[column1value] == <any value within>Array A[])
{
//do...
}

Cheers!

+8  A: 
if(myArray.Contains(A)){...}
Manu
Quick, easy. Thanks!
David Archer
This would use `Enumerable.Contains` extension method. Normally it's a perfectly appropriate solution, but it does incur a relatively small perf penalty because it works via `IEnumerable`. One can also use `Array.IndexOf(array, value) >= 0`, which only works on arrays, and is somewhat faster.
Pavel Minaev
A: 
YourArray.Any(item=>(item!=null) && item.Equals(yourvalue))
Gregoire
What about `null` items?
Pavel Minaev
A: 

You can try Array.Contains

EDIT.

Im sorry, thisis what i meant

int[] array = new int[] { 1, 2, 3, 4, 5 };
if (array.Contains(5))
{
}
astander
There's no `Array.Contains`.
Pavel Minaev
but there is ((ICollection)array).Contains(...)
erikkallen
+11  A: 

In .NET 3.5 or higher, using LINQ:

bool found = yourArray.Contains(yourValue);

In earlier versions of the framework:

bool found = Array.IndexOf(yourArray, yourValue) > -1;
LukeH
I like your answer better than the accepted one, because it answers situations in various versions.
Robert Koritnik
+1 Agree with Robert. This question is askeda great deal on stackoverflow, eh?
Daniel Elliott
Going to change... answer is same as previously chosen answer, but also provides other versions of framework. Nice one.
David Archer
A: 

If we're talking about pure Array type, there's IndexOf() method that will help you determine whether there's a value in it

Robert Koritnik
A: 

I believe the contains method is for lists.

(Did not see a way to respond to a specific previous comment, maybe because I am not a subscriber.)

chris