Class Expression
- Namespace
- YndigoBlue.Velocity.Model
- Assembly
- YndigoBlue.Velocity.dll
Represents a calculated expression in a SELECT clause using arithmetic operators, functions, and literals.
public class Expression : ISelectItem, IElement, IEnumerable
- Inheritance
-
Expression
- Implements
Remarks
Expressions allow you to perform calculations on column values in SELECT clauses. An expression is composed of elements (IElement) that can be:
- Columns - Database column references
- Literals - Constant values
- ArithmeticOperators - +, -, *, /, %
- Functions - Database functions like ROUND(), ABS(), etc.
Expressions are evaluated by the database and can be aliased for use in result sets.
Examples
Simple arithmetic expression:
// Calculate total price: quantity * unit_price
Expression totalExpression = new Expression("total");
totalExpression.Add(ordersTable["quantity"]);
totalExpression.Add(new ArithmeticOperator(ArithmeticType.Multiply));
totalExpression.Add(ordersTable["unit_price"]);
var query = new Query().Select(totalExpression).From(ordersTable);
// SQL: SELECT quantity * unit_price AS total FROM orders
Complex expression with multiple operations:
// Calculate: (price * quantity) + shipping
Expression orderTotal = new Expression("order_total");
orderTotal.Add(ordersTable["price"]);
orderTotal.Add(new ArithmeticOperator(ArithmeticType.Multiply));
orderTotal.Add(ordersTable["quantity"]);
orderTotal.Add(new ArithmeticOperator(ArithmeticType.Add));
orderTotal.Add(ordersTable["shipping"]);
var query = new Query().Select(orderTotal).From(ordersTable);
// SQL: SELECT (price * quantity) + shipping AS order_total FROM orders
Expression with literal value:
// Add 10% markup: price * 1.1
Expression markupPrice = new Expression("markup_price");
markupPrice.Add(productsTable["price"]);
markupPrice.Add(new ArithmeticOperator(ArithmeticType.Multiply));
markupPrice.Add(new Literal<decimal>(1.1m));
var query = new Query().Select(markupPrice).From(productsTable);
// SQL: SELECT price * 1.1 AS markup_price FROM products
Expression with function:
// Wrap a computed value in a function: ABS(price - cost)
// Functions whose arguments are joined into a single expression (Abs, Ceiling, Floor,
// Sqrt, Length, ...) accept a composed sequence of columns, operators and literals.
Expression margin = new Expression("margin", new Abs([
productsTable["price"],
new ArithmeticOperator(ArithmeticType.Subtract),
productsTable["cost"]
]));
var query = new Query().Select(margin).From(productsTable);
// SQL: SELECT ABS(price - cost) AS margin FROM products
Functions that take a fixed argument list — such as Round, Substring, Mod and the date
functions — map their arguments positionally, so arithmetic cannot be added as loose
elements alongside the other arguments. Compose it as a nested Expression
and pass that as the argument instead. To produce ROUND(price * 1.1, 2):
Expression markup = new Expression([
productsTable["price"],
new ArithmeticOperator(ArithmeticType.Multiply),
new Literal<decimal>(1.1m)
]);
Expression finalPrice = new Expression("final_price", new Round(markup, 2));
var query = new Query().Select(finalPrice).From(productsTable);
// SQL: SELECT ROUND(price * 1.1, 2) AS final_price FROM products
Constructors
- Expression()
Creates a new empty expression.
- Expression(IEnumerable<IElement>)
Creates a new expression with the specified elements and no name.
- Expression(string, IEnumerable<IElement>)
Creates a new expression with the specified name and elements.
- Expression(string, IElement)
Creates a new expression with the specified name and a single element.
- Expression(IElement)
Creates a new expression with a single element and no name.
Properties
- DataType
Gets the data type of this expression (always DataType.Expression).
- Elements
Gets the collection of elements (columns, operators, literals, functions) that make up this expression.
- Name
Gets the alias name for this expression in the result set.
- Precision
Gets the precision of the expression. Not applicable for expressions.
- Scale
Gets the scale of the expression. Not applicable for expressions.
- Size
Gets the size of the expression. Not applicable for expressions.
Methods
- Add(IElement)
Adds an element (column, operator, literal, or function) to this expression.
- GetEnumerator()
Returns an enumerator that iterates through the expression elements.