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

IsIn

Determines if an instance is contained in a sequence. Is the equivalent of Contains, but allows a more fluent reading "if item is in list", specially useful in LINQ extension methods like Where

Source

public static bool IsIn<T>(this T keyObject, IEnumerable<T> collection)
{
    return collection.Contains(keyObject);
}

Example

var inclusionList = new List<string> { "inclusion1", "inclusion2" };
var query = myEntities.MyEntity
.Select(e => e.Name)
.Where(e => e.IsIn(inclusionList));

Author: Joan Comas

Submitted on: 20 apr. 2016

Language: C#

Type: Generic

Views: 10564