Is there any way to pull out the properties, the operator and matching value from an Expression<Func<T>,bool>? Given the following example:
var customers = GetCustomers();
var customerQuery = customers.Where(x=> x.CustomerID == 1
&& x.CustomerName == "Bob"); // The query is for illustration only
I need to be able to get out something like the following:
Property: CustomerID
Operator: Equals
Value: 1
Property: CustomerName
Operator: Equals
Value: Bob
I’ve already written something that can pull out the property name of an Expression, but I cannot seem to find out where the value and operator are held, although it’s quite clearly visible in the Expression’s DebugView property.
The operator will be on the
BinaryExpression‘sMethodthat is theEqualsnode. You should also look at the expressions.NodeType, which reveals much (it should beEqual).The values will typically be in a
ConstantExpressionin the.Rightof thatBinaryExpression, or in the case of a captured variable: the capture-context will be theConstantExpression, so the value will be aMemberExpressionover aConstantExpression(you will need to investigate whether the member is aFieldInfovsPropertyInfo, and fetch the value via.GetValue(...)on that).