I’m using a Generic repository pattern, as displayed in this article.
These are my POCO Classes:
public class Order
{
public int ID { get; set; }
public int CustomerID {get; set;}
[InverseProperty("Order")]
public virtual List<OrderDetail> OrderDetail {get; set;}
public static Expression<Func<Order, bool>> OrdersFromCustomer(decimal customerId)
{
return f => f.CustomerID == customerId;
}
}
public class OrderDetail
{
public int OrderID { get; set;}
public int ID { get; set;}
public int ItemID { get; set;}
public int Amount { get; set;}
[ForeignKey("OrderID")]
public virtual Order { get; set;}
}
so when in my program I want to get all orders by a customer I can do this:
using (MyDbContext context = new MyDbContext())
{
MyRepository repository = new MyRepository(context);
var orders = repository.Get(Order.OrdersFromCustomer(25));
}
It works great, but I have an issue: if I want all the orders with Amount greater than 100? How could I build a Expression for filter on the details as the OrderFromCustomer function?
I’ve tried also with LinqKit but with no results.
You can just chain the Where filter clauses together, for example to do so in line it would look like: