Table of Contents

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

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"]);

query.AddSelectItem(totalExpression);

// SQL: SELECT quantity * unit_price AS total

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"]);

query.AddSelectItem(orderTotal);

// SQL: SELECT (price * quantity) + shipping AS order_total

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(1.1));

query.AddSelectItem(markupPrice);

// SQL: SELECT price * 1.1 AS markup_price

Expression with function:

// Round the result: ROUND(price * 1.1, 2)
Expression roundedMarkup = new Expression("final_price");
Function roundFunction = new Function(FunctionType.Round);
roundFunction.Add(productsTable["price"]);
roundFunction.Add(new ArithmeticOperator(ArithmeticType.Multiply));
roundFunction.Add(new Literal(1.1));
roundFunction.Add(new Literal(2)); // decimal places

roundedMarkup.Add(roundFunction);
query.AddSelectItem(roundedMarkup);

// SQL: SELECT ROUND(price * 1.1, 2) AS final_price

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.

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.