Wondering if it’s possible to create a dynamic linq query using linq to objects.
I have a screen where a user can filter on multiple things.
I Need to build an in memory filtering NOT using a database
So lets suppose I have a list of customers in memory and I would like to filter based on some multiselection.
I thought a method that I could pass a predicate would do the trick ,but obviously not.
How can I do it?
eg
public class Biz
{
public List<Customer> GetAllByName(string name)
{
return Repository.Find(x=> x.Name == name);
}
}
public class Repository
{
private List<Customer> internalCustomerList = GetAllCustomers();
public IEnumerable<Customer> Find(Expression<Func<T, bool>> predicate)
{
//How do I make below work in linq to object
return internalCustomerList.Where(predicate);//error here
}
}
You can do:
Please note that
List<T>does not have an overload forExpression<Func<T, bool>>. OnlyIQueryable<T>has. See more at http://msdn.microsoft.com/en-us/library/bb882637.aspx