Convert System. Data. DataTable computing

Source: Internet
Author: User

Based on an example, this article focuses on the computing function of System. Data. able. I believe this article will be helpful for your work and learning.
Using System;
Using System. ComponentModel;
Using System. Data;
Using System. Windows. Forms;
Namespace WindowsApplication1
...{
Public partial class Form1: Form
...{
Public Form1 ()
...{
InitializeComponent ();
}
Private void button#click (object sender, EventArgs e)
...{
// Detailed description of the System. Data. DataTable computing function. retained the following information
// Test environment vs2005, c #
System. Data. DataTable table = new DataTable ();
// Calculate the constant. No initialization column is allowed.
Object test = table. Compute ("1 + 1 ","");
Console. WriteLine (test );
// Test = 2;
Test = table. Compute ("1 + 1", "false ");
Console. WriteLine (test );
// Test = 2; constant calculation is irrelevant to filter
Test = table. Compute ("abs (1 )","");
Console. WriteLine (test );
// Test = null. I do not know why this is correct, and null is returned. Other mathematical functions will carry errors.
Test = table. Compute ("2% 2 ","");
Console. WriteLine (test );
// Test = 0;
// For other functions, refer to the following calculation column.
// Initialize datatale
Table. Columns. Add ("id", typeof (string ));
Table. Columns. Add ("value", typeof (int ));
For (int I = 1; I <= 10; I ++)
...{
System. Data. DataRow dRow = table. NewRow ();
DRow ["id"] = "id" + I. ToString ();
DRow ["value"] = I;
Table. Rows. Add (dRow );
}
// Test = table. Compute ("value + 1", "true ");
/** // Throw an exception, which must be an aggregate function
*****************//
// Calculate the quantity
Test = table. Compute ("count (id)", "false ");
Console. WriteLine (test );
// Test = 0;
Test = table. Compute ("count (id)", "true ");
Console. WriteLine (test );
// Test = 10;
// Sum
Test = table. Compute ("sum (value )","");
Console. WriteLine (test );
// Test = 55;
// Test = table. Compute ("sum (id )","");
/** // Throw an exception. It cannot be a string.
// Average
Test = table. Compute ("avg (value )","");
Console. WriteLine (test );
// Test = 5;
// Minimum
Test = table. Compute ("min (value )","");
Console. WriteLine (test );
// Test = 1;
// Maximum
Test = table. Compute ("max (value )","");
Console. WriteLine (test );
// Test = 10;
// Statistical standard deviation
Test = table. Compute ("StDev (value )","");
Console. WriteLine (test );
/// Test = 3.02765035409749
// Statistical variance
Test = table. Compute ("Var (value )","");
Console. WriteLine (test );
/// Test = 9.16666666666667
// Complex computing
Test = table. Compute ("max (value)/sum (value )","");
Console. WriteLine (test );
/// Test = 0.181818181818182
************** ****/
System. Data. DataColumn column = new DataColumn ("exp1", typeof (float ));
Table. Columns. Add (column );
// Simple computing
Column. Expression = "value * 2 ";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 2;
// String functions
Column. Expression = "len (id )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 3;
// String functions
Column. Expression = "len ('' + id + '')";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 5;
// String functions
Column. Expression = "len (trim ('' + id + ''))";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 3;
// String functions
Column. Expression = "substring (id, 3, len (id)-2 )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 1; // The starting character position of substring is 1, not 0
// Type conversion
Column. Expression = "convert (substring (id, 3, len (id)-2), 'System. Int32") * 1.6 ";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 1.6;
// Equivalent to isnull of sqlserver
Column. Expression = "isnull (value, 10 )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 1;
// Ternary operator, equivalent to the case when of sqlserver
Column. Expression = "iif (value> 5,1000, 2000 )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 2000;
// Like Operator
Column. Expression = "iif (id like '% 1', 1000,2000 )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 1000;
// In Operator
Column. Expression = "iif (id not in ('id1'), 1000,2000 )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 2000;
// Nested ternary operation
Column. Expression = "iif (value>, iif (id like '% 1 ))";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 4000;
// Percentage of client computing in total
Column. Expression = "value/sum (value )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 0.01818182
// The client calculates the difference, for example, the nba regular season victory.
Column. Expression = "max (value)-value ";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 9
// ********************** Parent-child table calculation ************* ***********/
// Initialize the child table and parent-child table relationships
DataTable tableChild = new DataTable ();
TableChild. Columns. Add ("id", typeof (string ));
TableChild. Columns. Add ("value", typeof (int ));
System. Data. DataSet ds = new DataSet ();
Ds. Tables. Add (tableChild );
Ds. Tables. Add (table );
DataRelation relation =
New DataRelation ("relation", table. Columns ["id"], tableChild. Columns ["id"]);
Ds. Relations. Add (relation );
For (int I = 1; I <= 10; I ++)
...{
System. Data. DataRow dRow = tableChild. NewRow ();
DRow ["id"] = "id1 ";
DRow ["value"] = I;
TableChild. Rows. Add (dRow );
}
// Calculate the number of sub-table records
Column. Expression = "count (child (relation). value )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test = 10;
// Calculate the percentage of parent and child tables
Column. Expression = "value/sum (child (relation). value )";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
/// Test = 0.01818182;
// Calculate the difference between the parent and child tables. For example, the parent table is the inventory quantity, and the sub-table is the order quantity. Then, calculate the quantity to be supplemented.
Column. Expression = "iif (value-sum
(Child (relation). value)> 0, value-sum (child (relation). value ))";
Test = table. Select ("id = 'id1'") [0] ["exp1"];
Console. WriteLine (test );
// Test =-54;
// Unfortunately, no method is found to calculate the year-on-year comparison and period-over-period comparison, and the calculation Column cannot be used as a constraint.
// The End Of ***********. DataTable allows you to use your talents as much as possible to reduce complexity.
SQL statement to reduce server computing compliance.
}
}
}

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.