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

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:

// 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<decimal>(1.1m));
roundFunction.Add(new Literal<int>(2)); // decimal places

roundedMarkup.Add(roundFunction);
var query = new Query().Select(roundedMarkup).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.