/// <summary> /// Returns and removes the first element in a sequence that satisfies both, /// the condition of the given predicate and the one contained in a query. /// WhereParser is used to evaluate the query. /// </summary> /// <returns>The first element in the sequence that passes the test in the specified predicate function.</returns> /// <param name="source">An IList<T> to return and remove an element from.</param> /// <param name="predicate">A query describing a function to test each element for a condition (ignored when null).</param> /// <param name="query">A query describing a function to test each element for a condition (ignored when null).</param> /// <typeparam name="TSource">The type of the elements of source.</typeparam> public static TSource PopFirst <TSource>(this List <TSource> source, Func <TSource, bool> predicate, string query) { if ((source == null) || ((predicate == null) && String.IsNullOrEmpty(query))) { throw new ArgumentNullException("Source or predicate and source are null."); } WhereParser.ConditionalStatement statement = WhereParser.Parse(query); if ((predicate == null) && (statement == null)) { throw new InvalidOperationException("No predicate was provided and query is null or is not in an acceptable format"); } else if (predicate == null) { return(source.PopFirst(o => statement.Evaluate(o))); } else if (statement == null) { return(source.PopFirst(predicate)); } else { return(source.PopFirst(o => predicate(o) && statement.Evaluate(o))); } }
/// <summary> /// Returns and removes the first element in a sequence that satisfies the condition contained in a query. /// WhereParser is used to evaluate the query. /// </summary> /// <returns>The first element in the sequence that passes the test in the specified predicate function.</returns> /// <param name="source">An IList<T> to return and remove an element from.</param> /// <param name="query">A query describing a function to test each element for a condition.</param> /// <typeparam name="TSource">The type of the elements of source.</typeparam> public static TSource PopFirst <TSource>(this List <TSource> source, string query) { WhereParser.ConditionalStatement statement = WhereParser.Parse(query); if (statement == null) { throw new InvalidOperationException("query is null or is not in an acceptable format"); } return(source.PopFirst(o => statement.Evaluate(o))); }