ExtensionMethod.NET Home of 820 C#, Visual Basic, F# and Javascript extension methods

In

Filters a list based on a comma-separated list of allowed values. This is a lot more concise than using a number of 'or' clauses

Source

public static bool In<T>(this T source,params T[] list)
{
   return list.ToList().Contains(source);
}

Example

var numbers = new List<int> { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };

//old way using or's
var old = numbers.Where(x=>x==2 || x==3 || x==5||x==7);

//new way using In
var primes = numbers.Where(x => x.In(2, 3, 5, 7));

Author: Chris Meijers

Submitted on: 25 okt 2010

Language: C#

Type: System.Collections.Generic.IEnumerable<T>

Views: 2691