Recently I was attempting to devise a way to filter down a large list with user generated conditions. These could be handled different ways depending on user input, so I couldn’t use a predefined lambda expression to filter the collection. What I wanted was a way to build an unknown number of lambda expressions from the user input and use them to filter the collection.
I ended up parsing the user input and adding each search expression to a List
List<TSource> sourceList; //Your source list to search. List<Func<TSource, bool>> expressions = new List<Func<TSource, bool>>(); //Build expressions from user input and add them to the expressions list. IEnumerable<TSource> results = sourceList; foreach (var expression in expressions) results = results.Where(expression); return results;