Use of compute in datasets

Source: Internet
Author: User

When you create an expression for a filter, enclose the string in single quotation marks:

"LastName = ' Jones '" characters below 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 characters above, the name must be enclosed in brackets. For example, to use a column named "column#" in an expression, write "[column#]": Total * [column#] because the brackets are special characters, if it is part of a column name, you must use a slash ("\") to escape the brackets. For example, a column named "column[" should be written as: Total * [column[\]] (only the second bracket must be escaped.) User-defined values user-defined values can be used in expressions that compare to column values. The value of the string should be enclosed in single quotation marks. The date value should be placed within the pound sign (#). For numeric values, decimals and scientific notation are allowed. For example: "FirstName = ' John '" "Price <= 50.00" "Birthdate < #1/31/82#" for a column that contains an enumeration value, cast the value to an integer data type. For example, the "Enumcolumn = 5" operator allows concatenation when using the Boolean and, or, and not operators. You can use parentheses to combine clauses and enforce precedence. The AND operator takes precedence over other operators. For example: (LastName = ' Smith ' OR LastName = ' Jones ') and FirstName = ' John ' When creating a comparison expression, the following operators are allowed: <><=>=<>= Inlike also supports the following arithmetic operators in expressions: + (plus)-(minus) * (multiply)/(except)% (modulo) string operators to concatenate strings, use the + character. The value of the CaseSensitive property of the DataSet class determines whether string comparisons are case-sensitive. However, you can override this value with the CaseSensitive property of the DataTable class. Wildcard characters in a like comparison, * and% can be used interchangeably as wildcards. If the string in the LIKE clause contains * or%, then these characters are escaped by applying the brackets ([]). If there is a bracket in the clause, the bracket character is applied in parentheses to escape it (for example [[] or []]). Wildcards are allowed at the beginning and end of the pattern, either at the end of the pattern, or at the beginning of the pattern. For example: "ItemnaMe like ' *product* ' "" ItemName like ' *product ' "" ItemName like ' product* ' "wildcard characters are not allowed in the middle of a string. For example, ' te*xt ' is not allowed. Parent/child relationship references you can refer to the parent table in an expression by adding parent in front of the column name. For example, Parent.price references a column named price for the parent table. By adding a child to the column name, you can refer to the columns in the sub-table in the expression. However, because a child relationship can return multiple rows, a reference to the child column must be included in the aggregation function. For example, SUM (child.price) returns the sum of the columns named price in the child table. If a table has multiple sub-tables, the syntax is: Child (RelationName). For example, if a table has two child tables whose names are Customers and Orders respectively, and the DataRelation object is named Customers2orders, the reference will be: AVG (customers2orders). Quantity) aggregation supports the following aggregation types: sum (sum) AVG (average) min (min) max (max) count (count) StDev (statistical standard deviation) Var (statistical variance). Aggregations are usually performed along a relationship. Create an aggregate expression by using one of the functions listed above and the child table columns detailed in "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 rollup of numbers in a column named "Price", use: Sum (price) A small application: Let the DataTable emulate Excel's formula functionality. The formula in Excel is powerful, and it's good to have a DataTable with similar functionality. Most of the event DataTable is used only as a data carrier, but it is seldom used for computation. The function compute provided by the DataTable provides powerful computational capabilities. The parameters of the COMPUTE function are two: Expression, and filter. Expresstion is a calculation expression, for more information about expression, see here "http://msdn2.microsoft.com/zh-cn/library/ System.data.datacolumn.expression (vs.80). aspx. Filter is a conditional filter, similar to SQL'sThe Where condition. 1: Here is the detailed description of expresstion. Let's look at the simplest usage, using aggregate functions. This aggregation function is a function commonly used in reports or Excel, such as Sum,avg and so on. For data columns, the DataTable can be easily computed, such as Datatable.cumpute ("Sum (column 1)", "Column 1>0"); For simple statistical functions, these functions are sufficient. (Other available functions are min,max,count, variance, standard deviation, etc.). This feature is often used, not surprisingly. Then look at the "Free expression" calculation. The calculation result can be obtained by writing the mathematical expression into a string and then directly putting into the COMPUTE function calculation. In this way, a dynamically generated calculation expression can be calculated. Let's say you want to calculate "column 1x30+20+2" or purely mathematical calculations. The compute function can also be calculated by calculating the concrete results. For example, this expression can return False:compute ("1=2", "true"); The use of this may be similar to the situation above, the result can be obtained when assembling some conditions freely. In Excel, the Logictest function is often used, and you can specify some logical expressions, such as: "IF (20>1000, 0, 1)". And how is this function to be implemented in a DataTable? It is also used compute, so write it: "IIF (20>1000, 0, 1)". In a DataTable, if you implement "if (c102=0,0,c105/c102*30)" You can use the above technical points. 2: As for the second parameter, filter, is a simple query condition. For example, "true", "id>4", "Name like '%nd ' and sex= ' Male '". The conditions cannot be complicated, and these are enough. Previously used Marge What is also good, can also be used for internal connection, in the small amount of data, need simple calculation, these functions are very useful. Of course, if the logic is extremely complex, this function of DT is not enough, or have to write their own logic (even if you want to implement "if (c102=0,0,c105/c102*30)", but also to write a bit of processing logic, but more general, write once just fine). private void Calccolumns () {    DataTable table = new DataTable ();   //Create the first column.&nbsp ; &nBsp DataColumn pricecolumn = new DataColumn ();    Pricecolumn.datatype = System.Type.GetType ("System.Decimal"); nbsp   Pricecolumn.columnname = "Price";    Pricecolumn.defaultvalue = 50;        //C reate the second, calculated, column.    DataColumn taxcolumn = new DataColumn ();    Taxcolumn.datatyp E = 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;} Transfer from http://biancheng.dnbcw.info/net/361686.html

Use of compute in the dataset

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.