Share via


Creating Expression Columns (ADO.NET)

You can define an expression for a column, enabling it to contain a value calculated from other column values in the same row or from the column values of multiple rows in the table. To define the expression to be evaluated, use the Expression property of the target column, and use the ColumnName property to refer to other columns in the expression. The DataType for the expression column must be appropriate for the value that the expression returns.

The following table lists several possible uses for expression columns in a table.

Expression type

Example

Comparison

"Total >= 500"

Computation

"UnitPrice * Quantity"

Aggregation

Sum(Price)

You can set the Expression property on an existing DataColumn object, or you can include the property as the third argument passed to the DataColumn constructor, as shown in the following example.

workTable.Columns.Add("Total",Type.GetType("System.Double"))
workTable.Columns.Add("SalesTax", Type.GetType("System.Double"), _
  "Total * 0.086")
workTable.Columns.Add("Total", typeof(Double));
workTable.Columns.Add("SalesTax", typeof(Double), "Total * 0.086");

Expressions can reference other expression columns; however, a circular reference, in which two expressions reference each other, will generate an exception. For rules about writing expressions, see the Expression property of the DataColumn class.

See Also

Reference

DataColumn

DataSet

DataTable

Other Resources

DataTable Schema Definition (ADO.NET)

DataTables (ADO.NET)

ADO.NET Managed Providers and DataSet Developer Center