Expression Column
Expression column is a Column<T> derived object with its Expression property returns non null value and its IsExpression returns true. The Expression property, of type ColumnExpression<T>, contains an expression tree for an expression column. This expression tree can be evaluated locally, or translated to native SQL expression by database providers.
Concrete column class normally provide members such as static methods, operator overloads and type converters so that you can use to construct expression column. For example, _Int32 provides following members:
- Static methods: Param and Const;
- Binary operator overrides:
+
,-
,*
,/
,%
,&
,|
,^
,==
,!=
,>
,<
,>=
,<=
- Unary operator overrides:
-
(negates) and~
(OnesComplement); - Type casting, either implicitly or explicitly, from underlying data type and other column types.
You can write expression using columns in a similar way of using their underlying data types. For example, the code UnitPrice * (_Decimal.Const(1) - UnitPriceDiscount) * OrderQty).IfNull(_Decimal.Const(0))
produce a expression column to calculate order subtotal as UnitPrice * (1 - UnitPriceDiscount) * OrderQty
.
Pay attention when writing expression column:
- Understand the difference between value expression created by
Param
(implicit conversion) andConst
(explicit conversion), which will create ParamExpression<T> and ConstantExpression<T> respectively. Be aware of possible SQL injection if you're usingConst
expression when the value is provided by user. - Since logical operator override is only allowed by boolean types, _Boolean column type overrides '&' and '|' instead of '&&' and '||'.
- Most column types override '==' and '!=' operator to compare underlying values. If you want to compare two column object reference, use
Object.ReferenceEquals
method instead.