SoFunction
Updated on 2025-03-07

Highlights of usage of Compute method in C# DataTable (numeric/string/operators/tables, etc.)

This article describes the usage of Compute method in C# DataTable. Share it for your reference, as follows:

There are two parameters of the Compute function: Expression and Filter.

Expression is a calculation expression. For details about Expression, please see here:

/zh-cn/library/(VS.80).aspx

Filter is a conditional filter, similar to SQL's Where conditions.

DataTable dt = new DataTable();
//Nested ternary operation, awesome to the five-body protrusionobject obj = ("iif(1000=5,1000,iif(100>100,4001,2000))", null);
(obj);
 table = new DataTable();
// Calculate constants, no initialization columns can beobject test = ("1+1", "");
(test);
string a = "123";
 b = 123;
decimal c = 123m;
((a));
//test=2;
test = ("1+1", "false");
(test);
//test=2; Constant calculation has nothing to do with filtertest = ("abs(1)", "");
(test);
//test=null, I don’t know why this has not reported an error, and if null is returned, other mathematical functions will be wrongtest = ("2%2", "");
(test);
//test=0;
//Refer to the calculation column below for other functions//Initialize datatale("id", typeof(string));
("value", typeof(int));
for (int i = 1; i <= 10; i++)
{
   dRow = ();
  dRow["id"] = "id" + ();
  dRow["value"] = i;
  (dRow);
}
//test = ("value+1", "true");
/**/
 ///// throw an exception, here it must be an aggregate function
 //***************************************Supported aggregate functions***************************//
//Check quantitytest = ("count(id)", "false");
(test);
//test=0;
test = ("count(id)", "true");
(test);
//test=10;
//Sumtest = ("sum(value)", "");
(test);
//test=55;
//test = ("sum(id)","");
/**/
 /////Exception is thrown, here cannot be string
 //average
 test = ("avg(value)", "");
 (test);
 //test=5;
 //Minimum
 test = ("min(value)", "");
 (test);
 //test=1;
 //maximum
 test = ("max(value)", "");
 (test);
 //test=10;
 //Statistical standard deviation
 test = ("StDev(value)", "");
 (test);
 //test=3.02765035409749
 //Statistical variance
 test = ("Var(value)", "");
 (test);
 //test=9.16666666666666667
 //Complex calculation
 test = ("max(value)/sum(value)", "");
 (test);
 //test=0.1818181818181818182
 /**/
/****************************************** Calculate column************************/
 column = new DataColumn("exp1", typeof(float));
(column);
//Simple calculation = "value*2";
test = ("id='id1'")[0]["exp1"];
(test);
//test=2;
//String function = "len(id)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=3;
//String function = "len(' '+id+' ')";
test = ("id='id1'")[0]["exp1"];
(test);
//test=5;
//String function = "len(trim(' '+id+' '))";
test = ("id='id1'")[0]["exp1"];
(test);
//test=3;
//String function = "substring(id,3,len(id)-2)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=1; //The starting character position of substring is 1 and not 0//Type conversion = "convert(substring(id,3,len(id)-2),'System.Int32')*1.6";
test = ("id='id1'")[0]["exp1"];
(test);
//test=1.6;
// isnull equivalent to sqlserver = "isnull(value,10)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=1;
//The ternary operator is equivalent to the case of SQLserver when = "iif(value>5,1000,2000)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=2000;
//like operator = "iif(id like '%1',1000,2000)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=1000;
//in operator = "iif(id not in('id1'),1000,2000)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=2000;
//Nested ternary operation = "iif(value>5,1000,iif(id like '%1',4000,2000))";
test = ("id='id1'")[0]["exp1"];
(test);
//test=4000;
//The percentage of total calculated by the client = "value/sum(value)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=0.01818182
//The client calculates the difference, such as the win difference in the NBA regular season = "max(value)-value";
test = ("id='id1'")[0]["exp1"];
(test);
//test=9
//******************************Father and Son Table Calculation*********************************/
//Initialize child table, father-son table relationshipDataTable tableChild = new DataTable();
("id", typeof(string));
("value", typeof(int));
 ds = new DataSet();
(tableChild);
(table);
DataRelation relation = new DataRelation("relation", ["id"], ["id"]);
(relation);
for (int i = 1; i <= 10; i++)
{
   dRow = ();
  dRow["id"] = "id1";
  dRow["value"] = i;
  (dRow);
}
// Calculate the number of records in the subtable = "count(child(relation).value)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=10;
//Calculate the percentage of the father and son table = "value/sum(child(relation).value)";
test = ("id='id1'")[0]["exp1"];
(test);
//test=0.01818182;
//Calculate the difference between the parent and child tables, such as the parent table is the inventory quantity and the child table is the order quantity, calculate the quantity that needs to be supplemented. = "iif(value-sum(child(relation).value)>0,0,value-sum(child(relation).value))";
test = ("id='id1'")[0]["exp1"];
(test);
//test=-54;
//It is unfortunate that there is no method that can calculate year-on-year and month-on-month, and the calculated column cannot be used as a constraint//End, DataTable allows you to use your intelligence as much as possible to reduce complicated SQL statements and reduce server computing compliance

For more information about C# related content, please check out the topic of this site:Tutorial on the usage of common C# controls》、《Summary of WinForm control usage》、《C# data structure and algorithm tutorial》、《Introduction to C# object-oriented programming tutorial"and"Summary of thread usage techniques for C# programming

I hope this article will be helpful to everyone's C# programming.