Table of Contents

Expression language

Rule and scoped-parameter expressions are parsed by System.Linq.Dynamic.Core, a C#-like language that is compiled to a delegate. This page summarizes what you will use most. Every example below was run against the library.

A rule expression must evaluate to bool. Inputs are available by name: input1, input2, ... or the names you gave with RuleParameter. The examples assume this input:

new RuleParameter("input1", new {
    country = "india", total = 6000, vip = false, name = "Asha", note = (string)null,
    items = new[] { new { Name = "pen", Price = 5 }, new { Name = "book", Price = 40 } }
});

Operators and members

Expression Result
input1.country == "india" AND input1.total >= 5000 true
input1.country = "india" true (a single = also means equality)
input1.name.StartsWith("As") && !input1.vip true
input1.country in ("india", "usa") true
input1.note == null OR input1.note.Length > 3 true
input1.total > 100 ? input1.vip : true false (conditional operator)
  • AND and OR can be written in words or as && and ||.
  • Strings use double quotes, so inside JSON they are escaped as \".
  • Member names and input names are not case-sensitive unless IsExpressionCaseSensitive is true (INPUT1.COUNTRY works), but string values are always compared exactly: "india" does not equal "INDIA".

Collections and LINQ

Inside the argument of a LINQ method, members of the current element can be used directly, or you can write an explicit lambda.

Expression Result
input1.items.Any(Price > 10) true
input1.items.Any(i => i.Price > 10) true
input1.items.Count() >= 2 true
input1.items.Where(Price >= 5).Count() == 2 true
input1.items.Sum(Price) == 45 true

See the System.Linq.Dynamic.Core documentation for the full list of sequence operators.

Calling your own code

Methods and static members are available only on registered types. Register them with ReSettings.CustomTypes:

{ "RuleName": "Allowed", "Expression": "MyHelpers.CheckContains(input1.country, \"india,usa\")" }

See the type security model. A method called in an expression must declare a concrete return type: if it returns object, member access and operators on its result cannot be resolved, and the error says so.

Returning objects (OutputExpression)

The OutputExpression action uses the same language. To return an object use new (value as Name, ...):

{ "Name": "OutputExpression", "Context": { "expression": "new (input1.total as Total, input1.country as Country)" } }

This returns an object with Total = 6000 and Country = "india". The C# form new { Total = input1.total } is not supported.

Scoped parameters

GlobalParams and LocalParams hold expressions in the same language. A scoped parameter can use the inputs and the scoped parameters defined before it. See Evaluation pipeline.

Troubleshooting expressions

Symptom Likely cause
Exception while parsing expression A syntax error, an unknown member or input name, or a call on an unregistered type. The message names the problem.
The rule fails with Error while executing rule A runtime error such as calling a method on null. Guard with x != null AND ....
A comparison with a property that can be null fails to parse The property was typed object because its value was null. See How inputs are typed.
Works with one input and not another A different input type compiles a separate delegate. Check the member names and types of the failing input.