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

ThrowIfAny

Throws a given exception is any value in a set passes a given predicate

Source

public static IEnumerable<T> ThrowIfAny<T>(this IEnumerable<T> values, Func<T, bool> predicate, Func<Exception> exceptionFunc)
{
    if (values.Any(val => predicate(val)))
        throw exceptionFunc();
    return values;
}

Example

categories.ThrowIfAny(c => c == CategoryTypes.Unknown, () => new Exception("The note category must be set."));

Author: Adam

Submitted on: 18 jul. 2013

Language: C#

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

Views: 4822