Scenario Dynamic Expressions

Modified on Thu, 11 Jul, 2013 at 12:57 PM

Infoborders Community Management (CM) Scenarios use dynamic expressions for syntax.  The information provided here describes the syntax supported by Dynamic Expressions.


Expression Language


The expression language implemented by the Dynamic Expression API provides a simple and convenient way of writing expressions that can be parsed into LINQ expression trees. The language supports most of the constructs of expression trees, but it is by no means a complete query or programming language. In particular, the expression language does not support statements or declarations.

The expression language is designed to be familiar to C#, VB, and SQL users. For this reason, some operators are present in multiple forms, such as && and and.

Literals

The expression language supports integer, real, string, and character literals.

An integer literal consists of a sequence of digits. The type of an integer literal is the first of the types Int32, UInt32, Int64, or UInt64 that can represent the given value. An integer literal implicitly converts to any other numeric type provided the number is in the range of that type. Some examples of integer literals:

0   123   10000

A real literal consists of an integral part followed by a fractional part and/or an exponent. The integral part is a sequence of one or more digits. The fractional part is a decimal point followed by one or more digits. The exponent is the letter e or E followed by an optional + or – sign followed by one or more digits. The type of a real literal is Double. A real literal implicitly converts to any other real type provided the number is in the range of that type. Some examples of real literals:

1.0   2.25   10000.0   1e0   1e10   1.2345E-4

A string literal consists of zero or more characters enclosed in double quotes. Inside a string literal, a double quote is written as two consecutive double quotes. The type of a string literal is String. Some examples of string literals:

"hello"   ""    """quoted"""   "'"

A character literal consists of a single character enclosed in single quotes. Inside a character literal, a single quote is written as two consecutive single quotes. The type of a character literal is Char. Some examples of character literals:

'A'   '1'   ''''   '"'

Constants

The predefined constants true and false denote the two values of the type Boolean.

The predefined constant null denotes a null reference. The null constant is of type Object, but is also implicitly convertible to any reference type.

Types

The expression language defines the following primitive types:

Object       Boolean      Char         String         SByte         Byte
Int16        UInt16       Int32        UInt32         Int64         UInt64
Decimal      Single       Double       DateTime       TimeSpan      Guid

The primitive types correspond to the similarly named types in the System namespace of the .NET Framework Base Class Library. The expression language also defines a set of accessible types consisting of the primitive types and the following types from the System namespace:

Math         Convert

The accessible types are the only types that can be explicitly referenced in expressions, and method invocations in the expression language are restricted to methods declared in the accessible types.

The nullable form of a value type is referenced by writing a ? after the type name. For example, Int32? denotes the nullable form of Int32.

The non-nullable and nullable forms of the types SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, and UInt64 are collectively called the integral types.

The non-nullable and nullable forms of the types Single, Double, and Decimal are collectively called the real types.

The integral types and real types are collectively called the numeric types.


Conversions

The following conversions are implicitly performed by the expression language:

  • From the the null literal to any reference type or nullable type.
  • From an integer literal to an integral type or real type provided the number is within the range of that type.
  • From a real literal to a real type provided the number is within the range of that type.
  • From a string literal to an enum type provided the string literal contains the name of a member of that enum type.
  • From a source type that is assignment compatible with the target type according to the Type.IsAssignableFrom method in .NET.
  • From a non-nullable value type to the nullable form of that value type.
  • From a numeric type to another numeric type with greater range.

The expression language permits explicit conversions using the syntax type(expr), where type is a type name optionally followed by ? and expr is an expression. This syntax may be used to perform the following conversions:

  • Between two types provided Type.IsAssignableFrom is true in one or both directions.
  • Between two types provided one or both are interface types.
  • Between the nullable and non-nullable forms of any value type.
  • Between any two types belonging to the set consisting of SByte, Byte, Int16, UInt16, Int32, UInt32, Int64, UInt64, Decimal, Single, Double, Char, any enum type, as well as the nullable forms of those types.

Operators

The table below shows the operators supported by the expression language in order of precedence from highest to lowest. Operators in the same category have equal precedence. In the table, x, y, and z denote expressions, T denotes a type, and m denotes a member.


Category

Expression

Description

Primary

x.m

Instance field or instance property access. Any public field or property can be accessed.

x.m(…)

Instance method invocation. The method must be public and must be declared in an accessible type.

x[…]

Array or indexer access. Multi-dimensional arrays are not supported.

T.m

Static field or static property access. Any public field or property can be accessed.

T.m(…)

Static method invocation. The method must be public and must be declared in an accessible type.

T(…)

Explicit conversion or constructor invocation. Note thatnew is not required in front of a constructor invocation.

new(…)

Data object initializer. This construct can be used to perform dynamic projections.

it

Current instance. Incontexts where members of a current object are implicitly in scope, it is used to refer to the entire object itself.

x(…)

Dynamic lambda invocation. Used to reference another dynamic lambda expression.

iif(x, y, z)

Conditional expression. Alternate syntax for x ? y : z.

Unary

-x

Negation. Supported types are Int32, Int64, Decimal,Single, and Double.

!x

not x

Logical negation. Operand must be of type Boolean.

Multiplicative

x * y

Multiplication. Supported types are Int32, UInt32, Int64,UInt64, Decimal, Single, and Double.

x / y

Division. Supported types are Int32, UInt32, Int64,UInt64, Decimal, Single, and Double.

x % y

x mod y

Remainder. Supported types are Int32, UInt32, Int64,UInt64, Decimal, Single, and Double.

Additive

x + y

Addition or string concatenation. Performs string concatenation if either operand is of type String. Otherwise, performs addition for the supported typesInt32, UInt32, Int64, UInt64, Decimal, Single, Double,DateTime, and TimeSpan.

x – y

Subtraction. Supported types are Int32, UInt32, Int64,UInt64, Decimal, Single, Double, DateTime, andTimeSpan.

x & y

String concatenation. Operands may be of any type.

Relational

x = y

x == y

Equal. Supported for reference types and the primitive types. Assignment is not supported.

x != y

x <> y

Not equal. Supported for reference types and the primitive types.

x < y

Less than. Supported for all primitive types exceptBoolean, Object and Guid.

x > y

Greater than. Supported for all primitive types exceptBoolean, Object and Guid.

x <= y

Less than or equal. Supported for all primitive types except Boolean, Object and Guid.

x >= y

Greater than or equal. Supported forall primitive types except Boolean, Object and Guid.

Logical AND

x && y

x and y

Logical AND. Operands must be of type Boolean.

Logical OR

x || y

x or y

Logical OR. Operands must be of type Boolean.

Conditional

x ? y : z

Evaluates y if x is true, evaluates z if x is false.




Was this article helpful?

That’s Great!

Thank you for your feedback

Sorry! We couldn't be helpful

Thank you for your feedback

Let us know how can we improve this article!

Select at least one of the reasons
CAPTCHA verification is required.

Feedback sent

We appreciate your effort and will try to fix the article