Table of Contents

Class Sql.ExpressionAttribute

Namespace
LinqToDB
Assembly
linq2db.dll

An Attribute that allows custom Expressions to be defined for a Method used within a Linq Expression.

[Serializable]
[AttributeUsage(AttributeTargets.Method|AttributeTargets.Property, AllowMultiple = true, Inherited = false)]
public class Sql.ExpressionAttribute : MappingAttribute, _Attribute
Inheritance
Sql.ExpressionAttribute
Implements
Derived
Inherited Members
Extension Methods

Constructors

ExpressionAttribute(string?)

Creates an Expression that will be used in SQL, in place of the method call decorated by this attribute.

public ExpressionAttribute(string? expression)

Parameters

expression string

The SQL expression. Use {0},{1}... for parameters given to the method call.

ExpressionAttribute(string, params int[])

Creates an Expression that will be used in SQL, in place of the method call decorated by this attribute.

public ExpressionAttribute(string expression, params int[] argIndices)

Parameters

expression string

The SQL expression. Use {0},{1}... for parameters given to the method call.

argIndices int[]

Used for setting the order of the method arguments being passed into the function.

ExpressionAttribute(string, string)

Creates an Expression that will be used in SQL, for the ProviderName specified, in place of the method call decorated by this attribute.

public ExpressionAttribute(string configuration, string expression)

Parameters

configuration string

The Database configuration for which this Expression will be used.

expression string

The SQL expression. Use {0},{1}... for parameters given to the method call.

ExpressionAttribute(string, string, params int[])

Creates an Expression that will be used in SQL, for the ProviderName specified, in place of the method call decorated by this attribute.

public ExpressionAttribute(string configuration, string expression, params int[] argIndices)

Parameters

configuration string

The Database configuration for which this Expression will be used.

expression string

The SQL expression. Use {0},{1}... for parameters given to the method call.

argIndices int[]

Used for setting the order of the method arguments being passed into the function.

Fields

UnknownExpression

public static readonly SqlExpression UnknownExpression

Field Value

SqlExpression

Properties

ArgIndices

The order of Arguments to be passed into the function from the method call.

public int[]? ArgIndices { get; set; }

Property Value

int[]

CanBeNull

If true, result can be null

public bool CanBeNull { get; set; }

Property Value

bool

ExpectExpression

Used internally by Sql.ExtensionAttribute.

public bool ExpectExpression { get; set; }

Property Value

bool

Expression

The expression to be used in building the SQL.

public string? Expression { get; set; }

Property Value

string

IgnoreGenericParameters

if true, do not generate generic parameters.

public bool IgnoreGenericParameters { get; set; }

Property Value

bool

InlineParameters

If true inline all parameters passed into the expression.

public bool InlineParameters { get; set; }

Property Value

bool

IsAggregate

If true, this expression represents an aggregate result Examples would be SUM(),COUNT().

public bool IsAggregate { get; set; }

Property Value

bool

IsNullable

Used to determine whether the return type should be treated as something that can be null If CanBeNull is not explicitly set.

Default is Undefined, which will be treated as true

public Sql.IsNullableType IsNullable { get; set; }

Property Value

Sql.IsNullableType

IsPredicate

If true the expression is treated as a Predicate And when used in a Where clause will not have an added comparison to 'true' in the database.

public bool IsPredicate { get; set; }

Property Value

bool

IsPure

If true, it notifies SQL Optimizer that expression returns same result if the same values/parameters are used. It gives optimizer additional information how to simplify query. For example ORDER BY PureFunction("Str") can be removed because PureFunction function uses constant value. For example Random function is NOT Pure function because it returns different result all time. But expression CurrentTimestamp is Pure in case of executed query. DateAdd(DateParts, double?, DateTime?) is also Pure function because it returns the same result with the same parameters.

public bool IsPure { get; set; }

Property Value

bool

IsWindowFunction

If true, this expression represents a Window Function Examples would be SUM() OVER(), COUNT() OVER().

public bool IsWindowFunction { get; set; }

Property Value

bool

Precedence

Determines the priority of the expression in evaluation. Refer to Precedence.

public int Precedence { get; set; }

Property Value

int

PreferServerSide

If true a greater effort will be made to execute the expression on the DB server instead of in .NET.

public bool PreferServerSide { get; set; }

Property Value

bool

ServerSideOnly

If true The expression will only be evaluated on the database server. If it cannot, an exception will be thrown.

public bool ServerSideOnly { get; set; }

Property Value

bool

Methods

CalcCanBeNull(IsNullableType, IEnumerable<bool>)

public static bool? CalcCanBeNull(Sql.IsNullableType isNullable, IEnumerable<bool> nullInfo)

Parameters

isNullable Sql.IsNullableType
nullInfo IEnumerable<bool>

Returns

bool?

GetCanBeNull(ISqlExpression[])

protected bool GetCanBeNull(ISqlExpression[] parameters)

Parameters

parameters ISqlExpression[]

Returns

bool

GetExpression<TContext>(TContext, IDataContext, SelectQuery, Expression, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression>)

public virtual ISqlExpression? GetExpression<TContext>(TContext context, IDataContext dataContext, SelectQuery query, Expression expression, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression> converter)

Parameters

context TContext
dataContext IDataContext
query SelectQuery
expression Expression
converter Func<TContext, Expression, ColumnDescriptor, ISqlExpression>

Returns

ISqlExpression

Type Parameters

TContext

GetIsPredicate(Expression)

public virtual bool GetIsPredicate(Expression expression)

Parameters

expression Expression

Returns

bool

GetObjectID()

Returns mapping attribute id, based on all attribute options.

public override string GetObjectID()

Returns

string

PrepareArguments<TContext>(TContext, string, int[]?, bool, List<Expression?>, List<SqlDataType>?, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression?>)

public static ISqlExpression[] PrepareArguments<TContext>(TContext context, string expressionStr, int[]? argIndices, bool addDefault, List<Expression?> knownExpressions, List<SqlDataType>? genericTypes, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression?> converter)

Parameters

context TContext
expressionStr string
argIndices int[]
addDefault bool
knownExpressions List<Expression>
genericTypes List<SqlDataType>
converter Func<TContext, Expression, ColumnDescriptor, ISqlExpression>

Returns

ISqlExpression[]

Type Parameters

TContext

PrepareParameterValues<TContext>(TContext, MappingSchema, Expression, ref string?, bool, out List<Expression?>, bool, out List<SqlDataType>?, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression>)

public static void PrepareParameterValues<TContext>(TContext context, MappingSchema mappingSchema, Expression expression, ref string? expressionStr, bool includeInstance, out List<Expression?> knownExpressions, bool ignoreGenericParameters, out List<SqlDataType>? genericTypes, Func<TContext, Expression, ColumnDescriptor?, ISqlExpression> converter)

Parameters

context TContext
mappingSchema MappingSchema
expression Expression
expressionStr string
includeInstance bool
knownExpressions List<Expression>
ignoreGenericParameters bool
genericTypes List<SqlDataType>
converter Func<TContext, Expression, ColumnDescriptor, ISqlExpression>

Type Parameters

TContext

ResolveExpressionValues<TContext>(TContext, string, Func<TContext, string, string?, string?>)

public static string ResolveExpressionValues<TContext>(TContext context, string expression, Func<TContext, string, string?, string?> valueProvider)

Parameters

context TContext
expression string
valueProvider Func<TContext, string, string, string>

Returns

string

Type Parameters

TContext