DataColumn - Expression Property (System - Data) - Microsoft Docs
DataColumn - Expression Property (System - Data) - Microsoft Docs
Expression Property
Namespace: System.Data
Assemblies: System.Data.dll, netstandard.dll, System.Data.Common.dll
In this article
Definition
Examples
Remarks
Applies to
See also
Gets or sets the expression used to filter rows, calculate the values in a column, or
create an aggregate column.
C# = Copy
Property Value
String
An expression to calculate the value of a column, or create an aggregate column. The
return type of an expression is determined by the DataType of the column.
Exceptions
ArgumentException
The AutoIncrement or Unique property is set to true .
FormatException
When you are using the CONVERT function, the expression evaluates to a string, but the
string does not contain a representation that can be converted to the type parameter.
InvalidCastException
When you are using the CONVERT function, the requested cast is not possible. See the
Conversion function in the following section for detailed information about possible
casts.
ArgumentOutOfRangeException
When you use the SUBSTRING function, the start argument is out of range.
-Or-
When you use the SUBSTRING function, the length argument is out of range.
Exception
When you use the LEN function or the TRIM function, the expression does not evaluate
to a string. This includes expressions that evaluate to Char.
Examples
The following example creates three columns in a DataTable. The second and third
columns contain expressions; the second calculates tax using a variable tax rate, and the
third adds the result of the calculation to the value of the first column. The resulting
table is displayed in a DataGrid control.
C# = Copy
Remarks
One use of the Expression property is to create calculated columns. For example, to
calculate a tax value, the unit price is multiplied by a tax rate of a specific region.
Because tax rates vary from region to region, it would be impossible to put a single tax
rate in a column; instead, the value is calculated using the Expression property, as
shown in the Visual Basic code in the following section:
VB = Copy
DataSet1.Tables("Orders").Columns("OrderCount").Expression =
"Count(OrderID)"
Expression Syntax
When you create an expression, use the ColumnName property to refer to columns. For
example, if the ColumnName for one column is "UnitPrice", and another "Quantity", the
expression would be as follows:
"UnitPrice * Quantity"
7 Note
When you create an expression for a filter, enclose strings with single quotation marks:
"LastName = 'Jones'"
If a column name contains any non-alphanumeric characters or starts with a digit or
matches (case-insensitively) any of the following reserved words, it requires special
handling, as described in the following paragraphs.
And
Between
Child
False
In
Is
Like
Not
Null
Or
Parent
True
If a column name satisfies one of the above conditions, it must be wrapped in either
square brackets or the "`" (grave accent) quotes. For example, to use a column named
"Column#" in an expression, you would write either "[Column#]":
Total * [Column#]
or "`Column#`":
Total * `Column#`
If the column name is enclosed in square brackets then any ']' and '\' characters (but not
any other characters) in it must be escaped by prepending them with the backslash ("\")
character. If the column name is enclosed in grave accent characters then it must not
contain any grave accent characters in it. For example, a column named "Column[]\"
would be written:
Total * [Column[\]\\]
or
Total * `Column[]\`
User-Defined Values
"FirstName = 'John'"
For columns that contain enumeration values, cast the value to an integer data type. For
example:
"EnumColumn = 5"
All literal expressions must be expressed in the invariant culture locale. When DataSet
parses and converts literal expressions, it always uses the invariant culture, not the
current culture.
String literals are identified when there are single quotes surrounding the value. For
example:
'John'
Boolean literals are true and false; they are not quoted in expressions.
Real literals using scientific notation, such as 4.42372E-30, are parsed using
System.Double .
Real literals without scientific notation, but with a decimal point, are treated as
System.Decimal . If the number exceeds the maximum or minimum values supported by
System.Decimal , then it is parsed as a System.Double . For example:
Operators
Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use
parentheses to group clauses and force precedence. The AND operator has precedence
over other operators. For example:
When you create comparison expressions, the following operators are allowed:
<
>
<=
>=
<>
IN
LIKE
+ (addition)
- (subtraction)
* (multiplication)
/ (division)
% (modulus)
String Operators
To concatenate a string, use the + character. The value of the CaseSensitive property of
the DataSet class determines whether string comparisons are case-sensitive. However,
you can override that value with the CaseSensitive property of the DataTable class.
Wildcard Characters
Both the * and % can be used interchangeably for wildcard characters in a LIKE
comparison. If the string in a LIKE clause contains a * or %, those characters should be
enclosed in brackets ([]). If a bracket is in the clause, each bracket character should be
enclosed in brackets (for example [[] or []]). A wildcard is allowed at the start and end of
a pattern, or at the end of a pattern, or at the start of a pattern. For example:
Wildcard characters are not allowed in the middle of a string. For example, 'te*xt' is not
allowed.
When a child has more than one parent row, use Parent(RelationName).ColumnName.
For example, the Parent(RelationName).Price references the parent table's column
named Price via the relation.
If a table has more than one child, the syntax is: Child(RelationName) . For example, if a
table has two child tables named Customers and Orders , and the DataRelation object is
named Customers2Orders , the reference would be as follows:
Avg(Child(Customers2Orders).Quantity)
Aggregates
The following aggregate types are supported:
Sum (Sum)
Avg (Average)
Min (Minimum)
Max (Maximum)
Count (Count)
Avg(Child.Price)
Avg(Child(Orders2Details).Price)
Sum(Price)
7 Note
Data types can always be determined by examining the DataType property of a column.
You can also convert data types using the Convert function, shown in the following
section.
An aggregate can only be applied to a single column and no other expressions can be
used inside the aggregate.
Functions
type -- The .NET Framework type to which the value will be converted.
All conversions are valid with the following exceptions: Boolean can be coerced to and
from Byte , SByte , Int16 , Int32 , Int64 , UInt16 , UInt32 , UInt64 , String and itself only.
Char can be coerced to and from Int32 , UInt32 , String , and itself only. DateTime can
be coerced to and from String and itself only. TimeSpan can be coerced to and from
String and itself only.
LEN
Example: myDataColumn.Expression="Len(ItemName)"
ISNULL
IIF
Description Gets one of two values depending on the result of a logical expression.
TRIM
Description Removes all leading and trailing blank characters like \r, \n, \t, ' '
SUBSTRING
7 Note
You can reset the Expression property by assigning it a null value or empty string.
If a default value is set on the expression column, all previously filled rows are
assigned the default value after the Expression property is reset.
Applies to
.NET Core
3.1, 3.0, 2.2, 2.1, 2.0
.NET Framework
4.8, 4.7.2, 4.7.1, 4.7, 4.6.2, 4.6.1, 4.6, 4.5.2, 4.5.1, 4.5, 4.0, 3.5, 3.0, 2.0, 1.1
.NET Standard
2.1, 2.0
Xamarin.Android
7.1
Xamarin.iOS
10.8
Xamarin.Mac
3.0
See also
Using DataSets in ADO.NET
Yes No