I'm creating a proof of concept that uses Lambda/LINQ for dynamic where and orderby. The following code works for the where expression, but I can not figure out how to create an order by expression. For this example, if possible I would like to keep it simple; I would rather not write code that modifies an Expression Tree.
void Main()
{
DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0));
Expression<Func<Products, bool>> filter = d => d.CreatedDate > productSince && d.Price < 100 ;
List<Products> products = GetProducts(filter, Products);
Console.WriteLine(products);
}
private static List<Products> GetProducts(Expression<Func<Products, bool>> filter, Table<Products> Products)
{
var products = Products.Where(filter);
return products.ToList();
}
What I want is similar to the following but can not figure out the code to create the order by expression.
void Main()
{
DateTime productSince = DateTime.UtcNow.Subtract(new TimeSpan(1,30,0));
Expression<Func<Products, bool>> filter = d => d.CreatedDate > productSince && d.Price < 100 ;
Expression<Func<Products, ????>> orderBy = d => ??????;
List<Products> products = GetProducts(filter, orderBy, Products);
Console.WriteLine(products);
}
private static List<Products> GetProducts(Expression<Func<Products, bool>> filter,
Expression<Func<Products, ???>> orderBy, Table<Products> Products)
{
var products = Products.Where(filter).OrderBy(orderBy);
return products.ToList();
}
If you are wondering, I'm using LinqPad for this proof of concept.