In. net, except regular expressions are used to filter rows, calculate values in columns, or create aggregate columns.

Source: Internet
Author: User
Tags alphanumeric characters

Namespace: System. data Assembly: System. data (in System. data. dll) syntax VBC # C ++ F # JScript in a color-differentiated format copy to clipboard print Public Property Expression As String Get SetPublic Property Expression As StringGetSetpublic Expression string Expression {get; set;} public string Expression {get; set;} public: property String ^ Expression {String ^ get (); void set (String ^ value);} public: property String ^ Expression {String ^ get (); void set (String ^ Value);} member Expression: string with get, setmember Expression: string with get, set attribute value type: System .. ::. string is 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. The exception condition ArgumentException sets the AutoIncrement or Unique attribute to true. FormatException when the CONVERT function is used, the expression is calculated as a string, but this string does not contain the format that can be converted into a type parameter. When the CONVERT function is used for InvalidCastException, the force conversion requested 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 starting parameter is out of the range. -Or-when the SUBSTRING function is used, the length parameter is out of the range. When the LEN or TRIM function is used, the calculation result of the expression is not a string. This includes the Char expression. The comment Expression 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 place a single tax rate in a column. Therefore, the Expression attribute is used to calculate this value, as shown in the Visual Basic Code in the following section: 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: copy data to the clipboard in a color-differentiated format and print DataSet1.Tables ("Orders "). columns ("OrderCount "). expression = "Count (OrderID)" DataSet1.Tables ("Orders "). columns ("OrderCount "). expression = "Count (OrderID)" Expression syntax when creating an Expression, use the ColumnName attribute to reference the column. For example, if the ColumnName of a column is "UnitPrice" and the other is "Quantity", the expression will be "UnitPrice * Quantity", indicating that 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 '" If the column name contains any non-alphanumeric characters, starts with a number, or matches (case-insensitive) any of the following reserved words requires special processing, as described in the following section. And between sub-level False membership Is Like Not Null Or parent level True if the column name meets one of the above conditions, it must be included in square brackets Or in '(heavy note) in quotation marks. For example, to use a Column named "Column #" in an expression, you should write it as "[Column #]": Total * [Column #] or "'Column no '": total * 'column # 'If the Column name is enclosed in square brackets, it must be enclosed by all "]" and "\" characters (not any other character) add the backslash ("\") character before to skip these characters. If the column name is enclosed in the accent character, it cannot contain any accent character. For example, a Column named "Column [] \" should be written: total * [Column [\] or Total * 'column [] \ 'user-defined values can be used in expressions that compare with Column values. The string value should be enclosed in single quotes (each single quotation mark character in the string value must be added before it and escaped with another single quotation mark character ). The date value is enclosed by the pound sign (#) or single quotation mark ('), depending on the data provider. Decimal and scientific notation are allowed for numeric values. For example, "FirstName = 'john'" Price <= 50.00 "" Birthdate <#1/31/82 # "forcibly converts a value to an integer data type for a column containing enumerated values. For example, "EnumColumn = 5" is analyzing the text expression. All text expressions must be expressed using fixed region settings. When DataSet analyzes and converts a text expression, it always uses a fixed culture instead of the current one. When the value is enclosed in single quotes, the string text is identified. For example, 'john' Boolean texts are true and false; they are not referenced in expressions. Convert Integer text [+-]? [0-9] + is regarded as System. Int32, System. Int64, or System. Double. System. Double can lose precision based on the quantity. For example, if the number in the text is 2147483650, DataSet first attempts to parse the number into an Int32. Because the value is too large, this will not succeed. In this case, DataSet calls the digital analysis an Int64, and the analysis succeeds. If the text is a number greater than the maximum value of Int64, DataSet uses Double to analyze the text. Use the actual text of scientific notation (for example, 4.42372E-30) to use System. Double for analysis. True text that does not use scientific notation but uses the Decimal point is considered as System. Decimal. If the number exceeds the maximum or minimum value supported by System. Decimal, it is analyzed as System. Double. For example, 142526.144524 is converted to Decimal. 345262.78036719560925667 will be treated as Double. Use the AND, OR, and not operators to concatenate values. 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' allow the following operators when creating a comparison expression: <><>=<>= in like also supports the following arithmetic operators IN the expression: "+" (plus)-(minus) * (multiplication)/() % (modulus) string operator to connect a string, use + characters. The CaseSensitive attribute value of the DataSet Class determines whether the string comparison is case sensitive. However, you can use the CaseSensitive attribute of the DataTable class to override this value. * And % can be used as wildcards in LIKE comparison. If the string in the LIKE clause contains * or %, these characters should be contained in brackets. If the clause contains parentheses, each character should be included in 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 references a Parent table in an expression by adding Parent to the column name. For example, Parent. Price references the column named Price in the Parent table. When a child row has multiple parent rows, use the parent (RelationName). ColumnName. For example, Parent (RelationName). Price references a column named Price in the Parent table through a link. By adding a Child to the column name, you can reference columns in the Child 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) returns the Sum of the columns named Price in the subtable. If a table has multiple sub-tables, the syntax is Child (RelationName ). For example, if a table has two sub-tables named MERs and Orders, and the DataRelation object is named Customers2Orders, the reference will be Avg (Child (Customers2Orders ). quantity) Aggregation supports the following aggregation types: Sum (Sum) Avg (average) Min (minimum) Max (maximum) Count (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) to indicate that if a table is used 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 the nullNothingnullptr null reference (Nothing in Visual Basic ). The data type can always be determined by checking the DataType attribute of the column. You can also use the Convert function to Convert data types, as shown in the following section. The function also supports the following functions: CONVERT Converts a specific expression to a specified. NET Framework type. Syntax Convert (expression, type) parameter expression-the expression to be converted. Type-value is converted to the. NET Framework type. Example: myDataColumn. expression = "Convert (total, 'System. int32 ') "all conversions are valid, except in the following cases: boolean can only be converted to Byte, SByte, Int16, Int32, Int64, UInt16, UInt32, UInt64, and String. Char can only be forced between Int32, UInt32, String, and itself. DateTime can only be forced between String and itself. TimeSpan can only be forced between String and itself. LEN describes how to obtain the length of a string. The LEN (expression) parameter expression-the string to be calculated. For example, myDataColumn. Expression = "Len (ItemName)" ISNULL indicates to check the Expression and return the Expression that has been checked or the replacement value. Syntax: ISNULL (expression, replacementvalue) parameter expression-the expression to be checked. Replacementvalue-if the expression is nullNothingnullptr null reference (Nothing in Visual Basic), replacementvalue is returned. For example, myDataColumn. Expression = "IsNull (price,-1)" IIF indicates that one of the two values is obtained based on the results of the logical Expression. Syntax IIF (expr, truepart, falsepart) parameter expr-expression to be calculated. Value returned when the truepart-expression is true. Falsepart-the value returned when the expression is false. Example: myDataColumn. expression = "IIF (total> 1000, 'expensive ', 'Demo') TRIM indicates removing all leading and suffix space characters, such as \ r, \ n, \ t, ''syntax TRIM (expression) parameter expression-the expression to be tailored. SUBSTRING: obtains a SUBSTRING of the specified length starting from the specified vertex in the string. Syntax SUBSTRING (expression, start, length) parameter expression-source string of the SUBSTRING. Start-an integer at the start of the substring. Length-refers to the integer of the length of the substring. For example, myDataColumn. Expression = "SUBSTRING (phone, 7, 8)" indicates that you can assign a null or empty string to the Expression attribute to reset this attribute. If a default value is set for the Expression column, the default value is assigned to all previously filled rows after the Expression attribute is reset. The following example creates three columns in the DataTable. 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. VBC # C ++ F # JScript is copied to the clipboard in a color-differentiated format and printed Private Sub CalcColumns () Dim rate As Single =. 0862 dim table as DataTable = New DataTable 'create the first column. dim priceColumn As 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 DataTable 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 Private Sub CalcColumns () Dim rate As Single =. 0862 dim table as DataTable = New DataTable 'create the first column. dim priceColumn As 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 DataTable 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 private void CalcColumns () {DataTable table = new DataTable (); // Create the first column. dataColumn priceColumn = new DataColumn (); priceColumn. dataType = System. type. getType ("System. decimal "); priceColumn. columnName = "price"; priceColumn. defaultValue = 50; // Create the second, calculated, column. dataColumn taxColumn = new DataColumn (); taxColumn. dataType = System. type. getType ("System. decimal "); taxColumn. columnName = "tax"; taxColumn. expression = "price * 0.0862"; // Create third column. dataColumn totalColumn = new DataColumn (); totalColumn. dataType = 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;} private void CalcColumns () {DataTable table = new DataTable (); // Create the first column. dataColumn priceColumn = new DataColumn (); priceColumn. dataType = System. type. getType ("System. decimal "); priceColumn. columnName = "price"; priceColumn. defaultValue = 50; // Create the second, calculated, column. dataColumn taxColumn = new DataColumn (); taxColumn. dataType = System. type. getType ("System. decimal "); taxColumn. columnName = "tax"; taxColumn. expression = "price * 0.0862"; // Create third column.www.2cto.com DataColumn totalColumn = new DataColumn (); totalColumn. dataType = 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 ;}

Related Article

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.