tickets.Where(t => new[] {"Open",
"Active",
"Reopen",
"InActive"}.Any(x => x == t.status))
Anyメソッドの代わりにContainメソッドを使用することもできますが、実装する比較ロジックがある場合は、デフォルトの等式比較プログラムの代わりにAnyメソッドを使用してください。
また
INメソッドをサポートする拡張機能を実装します。
public static class Extensions
{
public static bool In<TItem>(this TItem source, Func<TItem, TItem, bool> comparer, IEnumerable<TItem> items)
{
return items.Any(item => comparer(source, item));
}
public static bool In<TItem, T>(this TItem source, Func<TItem, T> selector, IEnumerable<TItem> items)
{
return items.Select(selector).Contains(selector(source));
}
public static bool In<T>(this T source, IEnumerable<T> items)
{
return items.Contains(source);
}
public static bool In<TItem>(this TItem source, Func<TItem, TItem, bool> comparer, params TItem[] items)
{
return source.In(comparer, (IEnumerable<TItem>)items);
}
public static bool In<TItem, T>(this TItem source, Func<TItem, T> selector, params TItem[] items)
{
return source.In(selector, (IEnumerable<TItem>)items);
}
public static bool In<T>(this T source, params T[] items)
{
return source.In((IEnumerable<T>)items);
}
}
そして、このように使用します:
bool b;
b = 7.In(3, 5, 6, 7, 8); // true
b = "hi".In("", "10", "hi", "Hello"); // true
b = "hi".In("", "10", "Hi", "Hello"); // false
b = "hi".In((s1, s2) => string.Equals(s1, s2, StringComparison.OrdinalIgnoreCase), "", "10", "Hi"); // true
var tuples = new List<Tuple<int, string>>();
for (var i = 0; i < 10; i++)
{
tuples.Add(Tuple.Create(i, ""));
}
var tuple = Tuple.Create(3, "");
b = tuple.In(tup => tup.Item1, tuples); // true