| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
DataColumn.Expression PropertySystem.Data Namespace DataColumn Class Sets or retrieves the expresssion used to filter rows, calculate the values in a column, or create an aggregate column. Syntax
Property Value
The property is read/write with no default value. Exceptions
RemarksOne 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 given region. Since 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 below: myDataSet.Tables ( "Products ).Columns ( "tax" ).Expression = "UnitPrice * 0.086" A second use is to create an aggregate column. Similar to a calculated value, an aggregate performs an operation based on the entire set of rows in the DataTable. A simple example is to count the number of rows returned in the set, which is the method you would use to count the number of transactions completed by a particular salesperson, as shown in this Visual Basic code: myDataSet.Tables ( "Orders" ).Columns ( "OrderCount" ).Expression = "Count ( OrderID ) " EXPRESSION SYNTAXWhen creating 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: "UnitPrice * Quantity" When creating an expression for a filter, enclose strings with single quotes: "LastName = 'Jones'" The following characters are special characters and must be escaped, as explained below, if they are to be used in a column name:
If a column name contains one of the above characters, the name must be wrapped in brackets. For example to use a column named "Column#" in an expression, you would write " [ column# ] ": Total * [ column# ] Because brackets are special characters, you must use a slash ( "\" ) to escape the bracket, if it is part of a column name. For example, a column named "Column [ ] " would be written: Total * [ column [ \ ] ] ( Only the second bracket must be escaped. ) USER-DEFINED VALUESUser-defined values may be used within expressions to be compared against column values. String values should be enclosed within single quotes. Date values should be enclosed within pound signs ( # ). Decimals and scientific notation are permissible for numeric values. For example: "FirstName = 'John'" "Price <= 50.00" "Birthdate < #1/31/82#" For columns that contain enumeration values, cast the value to an integer data type. For example: "EnumColumn = 5" OPERATORSConcatenation 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: ( LastName = 'Smith' OR LastName = 'Jones' ) AND FirstName = 'John' When creating comparison expressions, the following operators are allowed:
The following arithmetic operators are also supported in expressions:
STRING OPERATORSTo concatenate a string, use the + character. Whether string comparisons are case-sensitive or not is determined by the value of the DataSet class's CaseSensitive property. However, you can override that value with the DataTable class's CaseSensitive property. WILDCARD CHARACTERSBoth the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ( [ ] ). If a bracket is in the clause, the bracket characters should be escaped in brackets ( e.g. [ [ ] or [ ] ] ). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example: "ItemName LIKE '*product*'" "ItemName LIKE '*product'" "ItemName LIKE 'product*'" Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed. PARENT/CHILD RELATION REFERENCINGA column in a child table may be referenced in an expresion by prepending the column name with "Child". For example, "Child.Price" would reference the column named Price in the child table. If a table has more than one child, the syntax is: Child ( RelationName ). For example, if a table has two child tables named Employee and Titles, and the DataRelation objects are named "Publishers2Employee" and "Publishers2Titles", the reference would be: Child ( Publishers2Employee ).fname Child ( Publishers2Titles ).title A parent table may be referenced in an expression by prepending the column name with "Parent". For example, the "Parent.Price" references the parent table's column named "Price". AGGREGATESThe following aggregate types are supported:
Aggregates are usually performed along relationships. Create an aggregate expression by using one of the functions listed above and a child table column as detailed in PARENT/CHILD RELATION REFERENCING above. For example: Avg ( Child.Price ) Avg ( Child ( Orders2Details ).Price ) An aggregate can also be performed on a single table. For example, to create a summary of figures in a column named "Price": Sum ( Price )
If a table has no rows, the aggregate functions will return a null reference. 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 below. FUNCTIONSThe following functions are also supported: CONVERT
Example: myColumn.Expression="Convert ( total, 'System.Int32' ) " 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: myColumn.Expression="Len ( ItemName ) " ISNULL
Example: myColumn.Expression="IsNull ( price, -1 ) " IIF
Example: myColumn.Expression = "Iif ( total>1000,'expensive','dear' ) TRIM
SUBSTRING
Example: myColumn.Expression = "SUBSTRING ( phone, 7, 8 ) "
ExampleThe following example initializes three coumns 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.
See Also |
| ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
Check out related books at Amazon
© 2000-2008 Rey Nuñez All rights reserved.
If you have any question, comment or suggestion
about this site, please send us a note
You can help support aspxtreme