Skip to content

ExpressionTreeBuilder

Roi Shabtai (saturn) edited this page Apr 30, 2020 · 3 revisions

ExpressionTreeBuilder converts string representing binary condition to Expression<Func<T, bool>>. When the expression is illegal it returns null.

Important notes:

  • Property name must be on the left side of the binary expression
  • Whenever you want to use the chars represented by the \W regular expression (' ', '-', '+', '=', '>', '<', '&', '|' and others), you must escape the value of the string using \"

The full supported logic is located on ExpressionTreeBuilderTests class.

Examples

Illegal Expressions (returns null)

  • "idsfdsadffffdsfsdf2" (missing equality)
  • "id 2" //missing eqaulity
  • "id == == 2" duplicate equlity
  • "id == 2 value1 ==32" missing evaluation
  • "\"this-is-id\" == id" property name must be located on the left side of the expression

Legal Expressions

  • "id == \"this-is-id\"" returns "x => (x.Id == \"this-is-id\")"
  • "id == 2", "x => (x.Id == \"2\")"
  • "(id == 2 || numericvalue <3 && stringValue ==a)" returns "x => ((x.Id == \"2\") OrElse ((x.NumericValue < 3) AndAlso (x.StringValue == \"a\")))"
  • "(id == 2 || numericvalue <3) && stringValue ==a" returns "x => (((x.Id == \"2\") OrElse (x.NumericValue < 3)) AndAlso (x.StringValue == \"a\"))"
  • "id == 2 || (numericvalue ==32 && stringValue ==a)" returns "x => ((x.Id == \"2\") OrElse ((x.NumericValue == 32) AndAlso (x.StringValue == \"a\")))"
  • "id == 2 || (numericvalue ==32 && stringValue ==a) || stringValue ==b" returns "x => ((x.Id == \"2\") OrElse (((x.NumericValue == 32) AndAlso (x.StringValue == \"a\")) OrElse (x.StringValue == \"b\")))"