Instead of working with Expression
objects inside the method, use Func
:
Func<Order, int> productOrderItems =
(Order o) => o.Items.Where(i => i.ProductID == productID)
.Sum(i => i.Quantity);
Then inside your switch
, you can compose another Lambda expression on top of productOrderItems
as your return value. It will automatically be converted to an Expression
, as Expression
and Func
are technically interchangeable:
switch (comparator) {
case Comparator.Equals:
return (Order o) => productOrderItems(o) == count;
break;
// etc.
}