Thursday, October 24, 2013

C# implementation of SQL IN statement


SQL functionality to mimic:
SELECT * FROM table WHERE number IN (16, 30, 78, 80)

C# example that will work similarly.
List<int> Numbers = new List<int> { 16, 30, 78, 80 };
int number = 78;
if (Numbers.Contains(number))
{
    //...
}

Note: To make the functionality more similar, a class could be created inheriting from List which exposes an "IN" method which would simply encapsulate the List.Contains method.

No comments: