< go > The Compute method in a DataTable in C # uses the collection

Source: Internet
Author: User
Tags mathematical functions sql server isnull

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. The filter is a conditional filter, similar to the Where condition of SQL .

[CSharp]View Plaincopyprint?
    1. DataTable dt = new DataTable ();
    2. Nested ternary operations fork to pleasantly surprised
    3. Object obj = Dt.compute ("IIf (1000=5,1000,iif (100>100,4001,2000))", null);
    4. Response.Write (obj);
    5. System.Data.DataTable table = new DataTable ();
    6. Calculate constants, you can not initialize columns
    7. Object test = Table.compute ("+", " ");
    8. Console.WriteLine (test);
    9. String a = "123";
    10. System.Double B = 123;
    11. Decimal c = 123m;
    12. Console.WriteLine (Convert.todecimal (a));
    13. test=2;
    14. Test = Table.compute ("+", "false");
    15. Console.WriteLine (test);
    16. test=2; constant calculation is independent of filter
    17. Test = Table.compute ("ABS (1)", " ");
    18. Console.WriteLine (test);
    19. Test=null, do not know for this what no error, and return null, other mathematical functions will be wrong
    20. Test = Table.compute ("2%2", " ");
    21. Console.WriteLine (test);
    22. test=0;
    23. Other functions refer to the following computed columns
    24. Initialize Datatale
    25. Table.  Columns.Add ("id", typeof (string));
    26. Table.  Columns.Add ("value", typeof (int));
    27. for (int i = 1; i <=; i++)
    28. {
    29. System.Data.DataRow Drow = table. NewRow ();
    30. drow["id"] = "id" + i.tostring ();
    31. drow["value"] = i;
    32. Table. Rows.Add (drow);
    33. }
    34. Test = Table.compute ("Value+1", "true");
    35. /**/
    36. Throws an exception, this must be an aggregate function
    37. Supported aggregation functions **********************//
    38. Ask for quantity
    39. Test = Table.compute ("Count (ID)", "false");
    40. Console.WriteLine (test);
    41. test=0;
    42. Test = Table.compute ("Count (ID)", "true");
    43. Console.WriteLine (test);
    44. test=10;
    45. Sum
    46. Test = Table.compute ("sum (value)", " ");
    47. Console.WriteLine (test);
    48. test=55;
    49. Test = Table.compute ("sum (ID)", "");
    50. /**/
    51. Throws an exception, this cannot be a string
    52. Average
    53. Test = Table.compute ("avg (value)", " ");
    54. Console.WriteLine (test);
    55. test=5;
    56. Minimum
    57. Test = Table.compute ("min (value)", " ");
    58. Console.WriteLine (test);
    59. Test=1;
    60. Biggest
    61. Test = Table.compute ("max (value)", " ");
    62. Console.WriteLine (test);
    63. test=10;
    64. Statistical standard deviation
    65. Test = Table.compute ("StDev (value)", " ");
    66. Console.WriteLine (test);
    67. test=3.02765035409749
    68. Statistical variance
    69. Test = Table.compute ("Var (value)", " ");
    70. Console.WriteLine (test);
    71. test=9.16666666666667
    72. Complex calculations
    73. Test = Table.compute ("max (value)/sum (value)", " ");
    74. Console.WriteLine (test);
    75. test=0.181818181818182
    76. /**/
    77. /******************************************* computed column *************************/
    78. System.Data.DataColumn column = new DataColumn ("Exp1", typeof (float));
    79. Table. Columns.Add (column);
    80. Simple calculation
    81. Column.  Expression = "Value*2";
    82. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    83. Console.WriteLine (test);
    84. test=2;
    85. String functions
    86. Column.  Expression = "len (id)";
    87. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    88. Console.WriteLine (test);
    89. test=3;
    90. String functions
    91. Column.  Expression = "len (' +id+ ')";
    92. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    93. Console.WriteLine (test);
    94. test=5;
    95. String functions
    96. Column.  Expression = "len (Trim (' +id+ ')");
    97. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    98. Console.WriteLine (test);
    99. test=3;
    100. String functions
    101. Column.  Expression = "substring (Id,3,len (ID)-2)";
    102. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    103. Console.WriteLine (test);
    104. Test=1; The starting character position of the substring is 1, not 0.
    105. Type conversions
    106. Column.  Expression = "Convert (substring (id,3,len (ID)-2), ' System.Int32 ') *1.6";
    107. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    108. Console.WriteLine (test);
    109. test=1.6;
    110. Equivalent to SQL Server IsNull
    111. Column.  Expression = "IsNull (value,10)";
    112. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    113. Console.WriteLine (test);
    114. Test=1;
    115. Ternary operator, equivalent to SQL Server case
    116. Column.  Expression = "IIf (value>5,1000,2000)";
    117. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    118. Console.WriteLine (test);
    119. test=2000;
    120. Like operator
    121. Column.  Expression = "iif (Id like '%1 ', 1000,2000)";
    122. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    123. Console.WriteLine (test);
    124. test=1000;
    125. In operator
    126. Column.  Expression = "iif (id not in (' Id1 '), 1000,2000)";
    127. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    128. Console.WriteLine (test);
    129. test=2000;
    130. Nested ternary operations
    131. Column.  Expression = "IIf (Value>5,1000,iif (id like '%1 ', 4000,2000)");
    132. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    133. Console.WriteLine (test);
    134. test=4000;
    135. Percentage of total Client Computing
    136. Column.  Expression = "Value/sum (value)";
    137. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    138. Console.WriteLine (test);
    139. test=0.01818182
    140. The client calculates the difference, such as the win field difference in the NBA regular season
    141. Column.  Expression = "Max (value)-value";
    142. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    143. Console.WriteLine (test);
    144. Test=9
    145. Parent-child table calculation *************************************/
    146. Initializing child tables, parent-child table relationships
    147. DataTable Tablechild = new DataTable ();
    148. TABLECHILD.COLUMNS.ADD ("id", typeof (string));
    149. TABLECHILD.COLUMNS.ADD ("value", typeof (int));
    150. System.Data.DataSet ds = new DataSet ();
    151. Ds. Tables.add (Tablechild);
    152. Ds. Tables.add (table);
    153. DataRelation relation = new DataRelation ("Relation", table.  columns["id"], tablechild.columns["id"]);
    154. Ds. Relations.Add (relation);
    155. for (int i = 1; i <=; i++)
    156. {
    157. System.Data.DataRow drow = Tablechild.newrow ();
    158. drow["id"] = "ID1";
    159. drow["value"] = i;
    160. TABLECHILD.ROWS.ADD (drow);
    161. }
    162. Calculate the number of child table records
    163. Column.  Expression = "count (Child (relation). Value)";
    164. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    165. Console.WriteLine (test);
    166. test=10;
    167. Calculate the percentage of a parent-child table
    168. Column.  Expression = "Value/sum (Child (relation). Value)";
    169. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    170. Console.WriteLine (test);
    171. test=0.01818182;
    172. Calculates the difference between a parent and child table, such as the parent table as inventory quantity, the child table is the ordered quantity, calculates the quantity that needs to be replenished
    173. Column.  Expression = "IIf (Value-sum (relation). Value) >0,0,value-sum (Child (relation). Value)";
    174. Test = table.  Select ("id= ' id1 '") [0]["EXP1"];
    175. Console.WriteLine (test);
    176. test=-54;
    177. Unfortunately, there is no way to calculate the year and the quarter, and the computed column cannot be used as a constraint
    178. At the end, the DataTable allows you to make the most of your intelligence to reduce complex SQL statements and reduce server computing compliance

< go > The Compute method in a DataTable in C # uses the collection

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.