Datacolumn. Expression attributes

Source: Internet
Author: User
Datacolumn. Expression attributes
Net Framework class library

Datacolumn. Expression attributes

Gets or sets an expression for filtering rows, calculating values in a column, or creating an aggregate column.

Namespace:System. Data
ProgramSet:System. Data (in system. Data. dll)

Syntax Visual Basic (Declaration)

 
Public PropertyExpressionAs String

Visual Basic (usage)

DimInstanceAsDatacolumnDimValueAs StringValue = instance. Expression instance. Expression = Value

C #

 
Public StringExpression {Get;Set;}

C ++

Public: Property string ^ expression {string ^Get();Void Set(String ^ value );}

J #

 
/** @ Property */PublicString get_expression ()/** @ property */Public VoidSet_expression (string value)

JScript

Public Function GetExpression (): StringPublic Function SetExpression (value: string)

 

 

Attribute Value

Used to calculate the column value or create an aggregate column expression. The return type of the expression is determined by the datatype of the column.

Exception
Exception type Condition

Argumentexception

Set the autoincrement or unique attributeTrue.

Formatexception

When the convert function is used, the expression is calculated as a string, but this string does not contain the representation that can be converted to a type parameter.

Invalidcastexception

When the convert function is used, the requested forced conversion cannot be completed. For more information about possible forced conversions, see the Conversion Function in the following section.

Argumentoutofrangeexception

When using the substring function, the start parameter is out of the range.

-Or-

When the substring function is used, the length parameter is out of the range.

Exception

When using the Len function or trim function, the calculation result of the expression is not a string. This includes the char expression.

Remarks

ExpressionAn attribute is used to create calculated columns. For example, to calculate the tax value, you need to multiply the unit price by the tax rate of a specific region. Because the tax rates vary across regions, it is impossible to put a single tax rate in one column.ExpressionAttribute to calculate this value, as shown in the following section of Visual BasicCodeAs shown in:

Dataset1.tables ("Products"). Columns ("tax"). Expression = "unitprice * 0.086"

The second purpose is to create an aggregate column. Similar to the calculated value, aggregation performs operations based on the entire row set in the datatable. A simple example is to calculate the number of rows returned in the set. This is how you calculate the number of transactions completed by a specific salesperson, as shown in the following Visual Basic code:

 
Dataset1.tables ("orders"). Columns ("ordercount"). Expression = "count (orderid )"

Expression syntax

When creating an expression, use the columnname attribute to reference the column. For exampleColumnnameIs "unitprice", and the other is "quantity", the expression will be:

"Unitprice * quantity"

Note:

If a column is used in the expression, the expression has a dependency on the column. No exception is thrown when you rename or remove a dependency column. When you access an expression column that lacks dependencies, an exception is thrown.

When creating an expression for a filter, enclose the string in single quotes:

"Lastname = 'Jones '"

The following characters are special characters, as explained below. If they are used in column names, they must be escaped:

\ N (newline)

\ T (Tab)

\ R (carriage return)

~

(

)

#

\

/

=

>

<

+

-

*

%

&

|

^

'

"

[

]

If the column name contains one of the preceding characters, the name must be enclosed in brackets. For example, to use a column named "column #" in an expression, you should write it as "[column #]":

Total * [column #]

Since brackets are special characters, if they are an integral part of a column name, you must use a slash ("\") to escape the brackets. For example, a column named "column []" should be written:

Total * [column [\]

(Only the second brackets must be escaped .)

User-Defined Value

User-Defined values can be used in expressions that compare column values. The string value should be enclosed in single quotes. The date value should be placed in the pound sign. Decimal and scientific notation are allowed for numeric values. For example:

"Firstname = 'john '"

"Price <= 50.00"

"Birthdate <#1/31/82 #"

For columns that contain enumerated values, convert the values to integer data types. For example:

"Enumcolumn = 5"

Operator

Concatenation is allowed when the and, or, and not operators are used. You can use parentheses to combine clauses and force priorities. The and operator takes precedence over other operators. For example:

(Lastname = 'Smith 'or lastname = 'Jones') and firstname = 'john'

When creating a comparison expression, you can use the following operators:

<

>

<=

> =

<>

=

In

Like

The following Arithmetic Operators are also supported in expressions:

+ (Plus)

-(Minus)

* (Multiplication)

/()

% (Module)

String Operators

To connect strings, use + characters. The casesensitive attribute value of the dataset Class determines whether the string comparison is case sensitive. However, you can useDatatableClass's casesensitive attribute overwrites this value.

Wildcard

In the like comparison, * and % can be exchanged as wildcards. If the string in the like clause contains * or %, these characters are escaped using brackets. If the clause contains brackets, the brackets are used to escape the brackets (for example, [[] or []). Wildcards are allowed at the beginning and end of a mode, at the end of a mode, or at the beginning of a mode. For example:

"Itemname like '* product *'"

"Itemname like '* product '"

"Itemname like 'product *'"

Wildcards are not allowed between strings. For example, 'TE * XT 'is not allowed '.

Parent/child relationship reference

Add a column nameParentTo reference the parent table in the expression. For example,Parent. PriceName of the referenced parent tablePrice.

AddChildTo reference columns in the sub-table in the expression. However, because the child relationship can return multiple rows, the reference to the Child column must be included in the aggregate function. For example,Sum (child. Price)The name of the returned sub-table isPriceThe total number of columns.

If a table has multiple sub-tables, the syntax is:Child (relationname). For example, if a table has two sub-tables, their names areMERsAndOrdersAnd the datarelation object is namedCustomers2orders, The reference will be:

AVG (Child (customers2orders). Quantity)

Aggregation

The following aggregation types are supported:

Sum (SUM)

AVG (average)

Min (minimum)

Max (maximum)

Count)

STDev (statistical standard deviation)

VaR (statistical variance ).

Aggregation is usually executed along the link. Create an aggregate expression by using one of the functions listed above and the child table column detailed in the parent/child relationship reference above. For example:

AVG (child. Price)

AVG (Child (orders2details). Price)

Aggregation can also be performed on a single table. For example, to create a summary for a number in a column named "price", use:

Sum (price)

Note:

If you use a table to create an aggregate, no combination function is available. On the contrary, all rows show the same value in the column.

If the table does not have rows, the aggregate function returns a null reference (nothing in Visual Basic ).

The data type can always be checked by the columnDatatypeAttribute. You can also use the convert function to convert data types, as shown in the following section.

Functions

The following functions are also supported:

Convert

 

Description

Converts a specific expression to a specified. NET Framework type.

Syntax

Convert (Expression,Type)

Parameters

Expression-Expression to be converted.

Type-The value is converted to the. NET Framework type.

Example: mydatacolumn. Expression = "convert (total, 'System. int32 ')"

All conversions are valid, except in the following cases:BooleanOnlyByte,Sbyte,Int16,Int32,Int64,Uint16,Uint32,Uint64,StringAnd itself.CharOnlyInt32,Uint32,StringAnd itself.DatetimeOnlyStringAnd itself.TimespanOnlyStringAnd itself.

Len

 

Description

Returns the length of a string.

Syntax

Len (Expression)

Parameters

Expression-String to be calculated.

Example: mydatacolumn. Expression = "Len (itemname )"

Isnull

 

Description

Check the expression and return the expression that has been checked or the replacement value.

Syntax

Isnull (Expression,Replacementvalue)

Parameters

Expression-Expression to be checked.

Replacementvalue-If the expression is null reference (nothing in Visual Basic), returnReplacementvalue.

Example: mydatacolumn. Expression = "isnull (price,-1 )"

IIF

 

Description

Obtain one of the two values based on the results of the logical expression.

Syntax

IIF (Expr,Truepart,Falsepart)

Parameters

Expr-Expression to be calculated.

Truepart-The value returned when the expression is true.

Falsepart-The expression is the value returned when the expression is false.

For example: mydatacolumn. Expression = "IIF (total> 1000, 'expension', 'Demo ')

Trim

 

Description

Remove leading and suffix space characters, such as \ r, \ n, \ t ,''

Syntax

Trim (Expression)

Parameters

Expression-The expression to be cropped.

Substring

 

Description

Obtains a substring of the specified length starting from the specified vertex in the string.

Syntax

Substring (Expression,Start,Length)

Parameters

Expression-Source string of the substring.

Start-An integer at the start of the substring.

Length-An integer of the length of the substring.

Example: mydatacolumn. Expression = "substring (phone, 7, 8 )"

Note:

You canExpressionAttribute is assigned a null or empty string to reset this attribute. If a default value is set for the expression columnExpressionThis default value is assigned to all previously filled rows.

Example

The following example showsDatatableCreate three columns. The second and third columns contain expressions; the second column uses variable tax rates to Calculate taxes, and the third column adds the calculated results to the values in the first column. The result table is displayed in the DataGrid Control.

Copy code in Visual Basic

 Private   Sub Calccolumns () Dim Rate As   Single =. 0862 Dim Table As Datatable = New Datatable 'Create the first column.              Dim PricecolumnAs Datacolumn = New Datacolumn With Pricecolumn. datatype = system. type. GetType ( "System. Decimal" ). Columnname = "Price" . Defaultvalue = 50 End   With              'Create the second, calculated, column.              Dim Taxcolumn As Datacolumn = New Datacolumn With Taxcolumn. datatype = system. type. GetType ( "System. Decimal" ). Columnname = "Tax" . Expression = "Price * 0.0862"              End   With              'Create third column              Dim Totalcolumn As Datacolumn = New Datacolumn With Totalcolumn. datatype = system. type. GetType ( "System. Decimal" ). Columnname ="Total" . Expression = "Price + Tax"              End   With              'Add columns to able              With Table. Columns. Add (pricecolumn). Add (taxcolumn). Add (totalcolumn) End   With              Dim Row As Datarow = table. newrow table. Rows. Add (ROW) Dim View As   New Dataview view. Table = TABLE datagrid1.datasource = View End   Sub             

C # copy code

 Private   Void Calccolumns () {datatable table = New Datatable (); // Create the first column. Datacolumn pricecolumn = New Datacolumn (); pricecolumn. ype = system. type. GetType ("system. Decimal"); pricecolumn. columnname = "price"; pricecolumn. defaultvalue = 50; // Create the second, calculated, column. Datacolumn taxcolumn = New Datacolumn (); taxcolumn. ype = system. type. GetType ("system. Decimal"); taxcolumn. columnname = "tax"; taxcolumn. Expression = "price * 0.0862 "; // Create third column. Datacolumn totalcolumn = New Datacolumn (); totalcolumn. ype = system. type. GetType ("system. Decimal"); totalcolumn. columnname = "Total"; totalcolumn. Expression = "Price + Tax "; // Add columns to datatable. Table. columns. add (pricecolumn); table. columns. add (taxcolumn); table. columns. add (totalcolumn); datarow ROW = table. newrow (); table. rows. add (ROW); dataview view =New Dataview (table); datagrid1.datasource = view ;}

Platform

Windows 98, Windows 2000 SP4, Windows CE, Windows Millennium Edition, Windows Mobile for Pocket PC, Windows Mobile for smartphone, Windows Server 2003, Windows XP Media Center edition, Windows XP Professional x64 Edition windows XP SP2, Windows XP Starter Edition

. NET Framework does not support all versions of each platform. For a list of supported versions, see system requirements.

Version Information. NET Framework

Supported by the following versions: 2.0, 1.1, and 1.0
. NET Compact framework

Supported by versions 2.0 and 1.0

See references

Datacolumn class
Datacolumn Member
System. Data namespace

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.