/// <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))); } }
internal static Condition Parse(string s, ref int cc) { // A where clause starts with an identifier followed by a binary operator // and ends with a value. The type pattern is: io[sn] char type; Condition condition = new Condition(); condition.PropertyName = WhereParser.ReadNext(s, ref cc, out type); if (type != 'i') { return(null); } condition.Operator = WhereParser.ReadNext(s, ref cc, out type); if (type != 'o') { return(null); } condition.Value = WhereParser.ReadNext(s, ref cc, out type); condition.ValueType = type; if (type.IsAnyOf('0', 'B', 'n', 's')) { return(condition); } return(null); }
/// <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))); }
private static string ReadWhereClauses(string s, ref int cc) { Scanner.SkipSpaces(s, ref cc); int bcc = cc; // First, read the "where" literal string char[] where = new char[] { 'w', 'h', 'e', 'r', 'e' }; foreach (char c in where) { if (Scanner.ReadChar(c, s, ref cc)) { continue; } cc = bcc; return(null); } // After the "where" literal string, the where clauses come string clauses = WhereParser.Fetch(s, ref cc); return(clauses); }