Table of Contents

Class SelectBuilder<TBuilder>

Namespace
FluentCommand.Query
Assembly
FluentCommand.dll

Provides a generic base class for building SQL SELECT statements with fluent, chainable methods.

public abstract class SelectBuilder<TBuilder> : WhereBuilder<TBuilder>, IStatementBuilder, IQueryBuilder where TBuilder : SelectBuilder<TBuilder>

Type Parameters

TBuilder

The type of the builder for fluent chaining.

Inheritance
WhereBuilder<TBuilder>
SelectBuilder<TBuilder>
Implements
Derived
Inherited Members

Constructors

SelectBuilder(IQueryGenerator, List<QueryParameter>, LogicalOperators)

Initializes a new instance of the SelectBuilder<TBuilder> class.

protected SelectBuilder(IQueryGenerator queryGenerator, List<QueryParameter> parameters, LogicalOperators logicalOperator = LogicalOperators.And)

Parameters

queryGenerator IQueryGenerator

The IQueryGenerator used to generate SQL expressions.

parameters List<QueryParameter>

The list of QueryParameter objects for the query.

logicalOperator LogicalOperators

The logical operator (LogicalOperators) to combine WHERE expressions. Defaults to And.

Properties

FromExpressions

Gets the collection of FROM table expressions for the SELECT statement.

protected HashSet<TableExpression> FromExpressions { get; }

Property Value

HashSet<TableExpression>

A HashSet<T> containing the FROM table expressions.

GroupExpressions

Gets the collection of group by expressions for the GROUP BY clause.

protected HashSet<GroupExpression> GroupExpressions { get; }

Property Value

HashSet<GroupExpression>

A HashSet<T> containing the group by expressions.

JoinExpressions

Gets the collection of join expressions for the SELECT statement.

protected HashSet<JoinExpression> JoinExpressions { get; }

Property Value

HashSet<JoinExpression>

A HashSet<T> containing the join expressions.

LimitExpressions

Gets the collection of limit expressions for the SELECT statement.

protected HashSet<LimitExpression> LimitExpressions { get; }

Property Value

HashSet<LimitExpression>

A HashSet<T> containing the limit expressions.

SelectExpressions

Gets the collection of select column expressions for the SELECT statement.

protected HashSet<ColumnExpression> SelectExpressions { get; }

Property Value

HashSet<ColumnExpression>

A HashSet<T> containing the select column expressions.

SortExpressions

Gets the collection of sort expressions for the ORDER BY clause.

protected HashSet<SortExpression> SortExpressions { get; }

Property Value

HashSet<SortExpression>

A HashSet<T> containing the sort expressions.

Methods

Aggregate(AggregateFunctions, string, string, string)

Adds an aggregate expression using the specified function and column name to the SELECT clause.

public TBuilder Aggregate(AggregateFunctions function, string columnName, string tableAlias = null, string columnAlias = null)

Parameters

function AggregateFunctions

The aggregate function to use (e.g., Sum).

columnName string

The name of the column.

tableAlias string

The alias of the table (optional).

columnAlias string

The alias for the column (optional).

Returns

TBuilder

The same builder instance for method chaining.

BuildStatement()

Builds the SQL SELECT statement using the current configuration.

public override QueryStatement BuildStatement()

Returns

QueryStatement

A QueryStatement containing the SQL SELECT statement and its parameters.

Column(string, string, string)

Adds a column expression with the specified name to the SELECT clause.

public TBuilder Column(string columnName, string tableAlias = null, string columnAlias = null)

Parameters

columnName string

The name of the column.

tableAlias string

The alias of the table (optional).

columnAlias string

The alias for the column (optional).

Returns

TBuilder

The same builder instance for method chaining.

ColumnIf(string, string, string, Func<string, bool>)

Conditionally adds a column expression with the specified name to the SELECT clause.

public TBuilder ColumnIf(string columnName, string tableAlias = null, string columnAlias = null, Func<string, bool> condition = null)

Parameters

columnName string

The name of the column.

tableAlias string

The alias of the table (optional).

columnAlias string

The alias for the column (optional).

condition Func<string, bool>

A function that determines whether to add the column, based on the column name. If null, the column is always added.

Returns

TBuilder

The same builder instance for method chaining.

Columns(IEnumerable<string>, string)

Adds a column expression for each of the specified column names to the SELECT clause.

public virtual TBuilder Columns(IEnumerable<string> columnNames, string tableAlias = null)

Parameters

columnNames IEnumerable<string>

The collection of column names.

tableAlias string

The alias of the table (optional).

Returns

TBuilder

The same builder instance for method chaining.

Exceptions

ArgumentNullException

Thrown if columnNames is null.

Count(string, string, string)

Adds a COUNT aggregate expression using the specified column name to the SELECT clause.

public TBuilder Count(string columnName = "*", string tableAlias = null, string columnAlias = null)

Parameters

columnName string

The name of the column (default is *).

tableAlias string

The alias of the table (optional).

columnAlias string

The alias for the column (optional).

Returns

TBuilder

The same builder instance for method chaining.

From(string, string, string)

Adds a FROM clause to the query.

public virtual TBuilder From(string tableName, string tableSchema = null, string tableAlias = null)

Parameters

tableName string

The name of the table.

tableSchema string

The schema of the table (optional).

tableAlias string

The alias of the table (optional).

Returns

TBuilder

The same builder instance for method chaining.

FromRaw(string)

Adds a raw FROM clause to the query.

public TBuilder FromRaw(string fromClause)

Parameters

fromClause string

The raw SQL FROM clause.

Returns

TBuilder

The same builder instance for method chaining.

From<TEntity>(string)

Adds a FROM clause to the query using the specified entity type.

public TBuilder From<TEntity>(string tableAlias = null)

Parameters

tableAlias string

The alias of the table (optional).

Returns

TBuilder

The same builder instance for method chaining.

Type Parameters

TEntity

The type of the entity.

GroupBy(string, string)

Adds a GROUP BY clause with the specified column name.

public TBuilder GroupBy(string columnName, string tableAlias = null)

Parameters

columnName string

The name of the column to group by.

tableAlias string

The alias of the table (optional).

Returns

TBuilder

The same builder instance for method chaining.

Join(Action<JoinBuilder>)

Adds a JOIN clause to the query using the specified builder action.

public TBuilder Join(Action<JoinBuilder> builder)

Parameters

builder Action<JoinBuilder>

An action that configures the join using a JoinBuilder.

Returns

TBuilder

The same builder instance for method chaining.

Limit(int, int)

Adds a LIMIT clause with the specified offset and size.

public TBuilder Limit(int offset = 0, int size = 0)

Parameters

offset int

The number of rows to skip before starting to return rows.

size int

The maximum number of rows to return.

Returns

TBuilder

The same builder instance for method chaining.

OrderBy(string, SortDirections)

Adds an ORDER BY clause with the specified column name and sort direction.

public TBuilder OrderBy(string columnName, SortDirections sortDirection = SortDirections.Ascending)

Parameters

columnName string

The name of the column to sort by.

sortDirection SortDirections

The sort direction (default is Ascending).

Returns

TBuilder

The same builder instance for method chaining.

OrderBy(string, string, SortDirections)

Adds an ORDER BY clause with the specified column name, sort direction, and table alias.

public TBuilder OrderBy(string columnName, string tableAlias, SortDirections sortDirection = SortDirections.Ascending)

Parameters

columnName string

The name of the column to sort by.

tableAlias string

The alias of the table (optional).

sortDirection SortDirections

The sort direction (default is Ascending).

Returns

TBuilder

The same builder instance for method chaining.

OrderByIf(string, SortDirections, Func<string, bool>)

Conditionally adds an ORDER BY clause with the specified column name and sort direction.

public TBuilder OrderByIf(string columnName, SortDirections sortDirection = SortDirections.Ascending, Func<string, bool> condition = null)

Parameters

columnName string

The name of the column to sort by.

sortDirection SortDirections

The sort direction (default is Ascending).

condition Func<string, bool>

A function that determines whether to add the ORDER BY clause, based on the column name. If null, the clause is always added.

Returns

TBuilder

The same builder instance for method chaining.

OrderByIf(string, string, SortDirections, Func<string, bool>)

Conditionally adds an ORDER BY clause with the specified column name, sort direction, and table alias.

public TBuilder OrderByIf(string columnName, string tableAlias, SortDirections sortDirection = SortDirections.Ascending, Func<string, bool> condition = null)

Parameters

columnName string

The name of the column to sort by.

tableAlias string

The alias of the table (optional).

sortDirection SortDirections

The sort direction (default is Ascending).

condition Func<string, bool>

A function that determines whether to add the ORDER BY clause, based on the column name. If null, the clause is always added.

Returns

TBuilder

The same builder instance for method chaining.

OrderByRaw(IEnumerable<string>)

Adds multiple raw ORDER BY clauses to the query.

public TBuilder OrderByRaw(IEnumerable<string> sortExpressions)

Parameters

sortExpressions IEnumerable<string>

A collection of raw SQL ORDER BY clauses.

Returns

TBuilder

The same builder instance for method chaining.

Exceptions

ArgumentNullException

Thrown if sortExpressions is null.

OrderByRaw(string)

Adds a raw ORDER BY clause to the query.

public TBuilder OrderByRaw(string sortExpression)

Parameters

sortExpression string

The raw SQL ORDER BY clause.

Returns

TBuilder

The same builder instance for method chaining.

OrderByRawIf(string, Func<string, bool>)

Conditionally adds a raw ORDER BY clause to the query.

public TBuilder OrderByRawIf(string sortExpression, Func<string, bool> condition = null)

Parameters

sortExpression string

The raw SQL ORDER BY clause.

condition Func<string, bool>

A function that determines whether to add the ORDER BY clause, based on the expression. If null, the clause is always added.

Returns

TBuilder

The same builder instance for method chaining.

Page(int, int)

Adds a LIMIT clause for paging with the specified page number and page size.

public TBuilder Page(int page = 0, int pageSize = 0)

Parameters

page int

The page number (1-based).

pageSize int

The number of rows per page.

Returns

TBuilder

The same builder instance for method chaining.