Reputation: 13686
How do I detect if a MemberExpression has a value that needs to be compiled/evaluated?
I have two separate member expression outputs, the first which has a value, and the second which doesn't. What is the best way to differentiate between the two?
exp
**{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
[System.Linq.Expressions.MemberExpression]: **{value(Microsoft.Connect.Api.Client.Tests.SearchQueryUnitTests+<>c__DisplayClass6).handle}**
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
vs
exp
{x.CreatedBy}
[System.Linq.Expressions.MemberExpression]: {x.CreatedBy}
NodeType: MemberAccess
Type: {Name = "String" FullName = "System.String"}
Upvotes: 2
Views: 418
Reputation: 74540
I see two different answers to this.
Are you trying to create a conditional with expressions and then do something with the result? If that is the case, you will create a BinaryExpression of type Equals (call the static Equals method on the Expression class) passing in the MemberExpression as left, and then passing another expression representing a non-value (null if a reference type, or a new instance of the struct if a value type).
If you are actually trying to evaluate the expression at that point, then I would create a lambda which actually returns the expression, compile it, and then check against a non-value in your code.
Upvotes: 1