ICustomFunctionOperatorFormattable.Format(Type, String[]) Method
Returns a string for insertion into a SQL query when the custom function is evaluated (before a query is sent to the data source).
Namespace: DevExpress.Data.Filtering
Assembly: DevExpress.Data.v24.2.dll
NuGet Package: DevExpress.Data
Declaration
Parameters
Name | Type | Description |
---|---|---|
providerType | Type | A Type object that identifies the data provider. If the data source of the query expression is Microsoft SQL Server, the type is MSSqlConnectionProvider. |
operands | String[] | An array of operands (parameters) passed to a custom function. |
Returns
Type | Description |
---|---|
String | A String that substitutes a call to the custom function in a query statement. |
Remarks
You can follow these steps to implement the Format
method:
- Find a suitable SQL command or syntax.
- Return a dummy value (operands[0]) to the Format function.
- Set a breakpoint at the Format method to inspect what values it receives.
- Continue execution to log the SQL command.
- Generate the string you would insert in this command to get the desired result.
- Implement the
Format
method so that it returns the desired string based on the current data provider.
Example
The following code implements the StdDev custom function intended only for SQL queries in Query Builder. The Format
method modifies the query string.
When this function is used in an expression, the Evaluate
method throws an exception.
using System;
using System.Collections.Generic;
using DevExpress.Data.Filtering;
namespace SelectQueryWindowsFormsApplication {
public sealed class StDevFunction : ICustomFunctionOperatorBrowsable, ICustomFunctionOperatorFormattable {
const string name = "StDev";
public static void Register() {
CriteriaOperator.RegisterCustomFunction(new StDevFunction());
}
public static void Unregister() {
CriteriaOperator.UnregisterCustomFunction(name);
}
public static readonly HashSet<Type> ValidOperandTypes = new HashSet<Type> {
typeof(sbyte),
typeof(byte),
typeof(short),
typeof(ushort),
typeof(int),
typeof(uint),
typeof(long),
typeof(ulong),
typeof(decimal),
typeof(double),
typeof(float)
};
#region ICustomFunctionOperator
public Type ResultType(params Type[] operands) {
return typeof(double);
}
public object Evaluate(params object[] operands) {
throw new NotSupportedException();
}
public string Name { get { return name; } }
#endregion
#region ICustomFunctionOperatorBrowsable
public FunctionCategory Category { get { return FunctionCategory.Math; } }
public string Description { get { return "Standard deviation function"; } }
public bool IsValidOperandCount(int count) {
return count == 1;
}
public bool IsValidOperandType(int operandIndex, int operandCount, Type type) {
return ValidOperandTypes.Contains(type);
}
public int MaxOperandCount { get { return 1; } }
public int MinOperandCount { get { return 1; } }
#endregion
#region ICustomFunctionOperatorFormattable
public string Format(Type providerType, params string[] operands) {
return string.Format("stdev({0})", operands[0]);
}
#endregion
}
}