Given an Expression<Func<TEntity, bool>>
along the lines of
entity => entity.SubEntity.Any(
subEntity => (
(subEntity.SomeProperty == False)
AndAlso
subEntity.SubSubEntity.FooProperty.StartsWith(
value(SomeClass+<>c__DisplayClass0).ComparisonProperty
)
AndAlso
subEntity.SubSubEntity.BarProperty == "Bar"
AndAlso
subEntity.SubSubEntity.SubSubSubEntity.Any(
subSubSubEntity => (x.SubSubSubSubEntity.BazProperty == "whatever")
)
)
)
I am trying to extract a list property conditions by type, i.e.
TEntity : [ /* no conditions for immediate members of TEntity */ ]
TSubEntity : [ { SomeProperty == False } ]
TSubSubEntity : [ { FooProperty.StartsWith(/* ... */) },
{ BarProperty == "Bar" } ],
TSubSubSubEntity : [ /* no conditions for immediate members of TSubSubSubEntity */ ],
TSubSubSubSubEntity : [ { BazProperty == "whatever" } ]
So far, I have created an ExpressionVisitor
and identified the VisitBinary
method as the one I want to plug into in order to obtain my information.
I am still at a loss about
- how to determine whether the
BinaryExpression
I am looking at represents a terminal statement (in the sense that there are no more nested expressions that I need to look at) - how to determine the Entity type that the
BinaryExpression
is concerned with - whether I need to override any of the other
ExpressionVisitor
methods to cover for cases that I have not considered yet.
Not sure what really is the use case, but here is some starting point
The idea is simple. Override
Visit
method just to maintain a stack of processing expressions. The main processing is inside theVisitMember
override, which is called for each property/field accessor. Thenode.Expression.NodeType != ExpressionType.Constant
is used to eliminate the closure members, while the second condition eliminates collection properties. Finally, the potential condition expression is extracted from the stack.The result is including both
MemberExpression
and theExpression
where it is used.MemberExpression.Expression.Type
is your entity type,MemberExpression.Member
is the property/field of that type.Sample test: