David Archer
David Archer

Reputation: 1164

Value equal to ANY value in an array?

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!

Upvotes: 4

Views: 7780

Answers (5)

Gregoire
Gregoire

Reputation: 24872

yourArray.Any(item => item != null && item.Equals(yourvalue));

Upvotes: 0

Adriaan Stander
Adriaan Stander

Reputation: 166566

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

Upvotes: 0

Robert Koritnik
Robert Koritnik

Reputation: 105081

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

Upvotes: 0

LukeH
LukeH

Reputation: 269628

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;

Upvotes: 14

Manu
Manu

Reputation: 29153

if(myArray.Contains(A)){...}

Upvotes: 8

Related Questions