I have a complex query where certain DateTime members of a model entity are being used multiple times, but not always the same members. I'd like to know if I can put the "getting" of the member in an Expression, so I don't have to repeat that piece of code.
A simplified version of my current code looks like this:
var getTargetDate = ((Expression<Func<Order, DateTime>>)(o => o.OrderDate)).Compile();
if (delivery)
getTargetDate = ((Expression<Func<Order, DateTime>>)(o => o.DeliveryDate)).Compile();
return orders.Where(o => getTargetDate(o) >= fromDate && getTargetDate(o) < toDate);
This code compiles, but gives me the runtime Exception:
Exception thrown: 'System.NotSupportedException' in System.Data.Linq.dll
Additional information: Method 'System.Object DynamicInvoke(System.Object[])' has no supported translation to SQL.
Is there a way to put the "getting" of the DateTime in a variable or method that can be translated to SQL?