static double Evaluate(string expression) { var loDataTable = new DataTable(); var loDataColumn = new DataColumn("Eval", typeof (double), expression); loDataTable.Columns.Add(loDataColumn); loDataTable.Rows.Add(0); return (double) (loDataTable.Rows[0]["Eval"]); }
An explanation of how this works:
First, we create a table in the part var loDataTable = new DataTable(); , as is the case with a database engine (for example, MS SQL).
Then a column with certain parameters ( var loDataColumn = new DataColumn("Eval", typeof (double), expression); ).
The "Eval" parameter is the name of the column (ColumnName attribute).
typeof (double) is the type of data that should be stored in the column, which is equal instead of System.Type.GetType("System.Double"); .
expression is the string that the Evaluate method receives and is stored in the expression attribute of the column. This attribute is intended for a really specific purpose (obviously), which means that every row placed in a column will be filled with an “expression”, and it will take almost everything that can be placed in an SQL query. See http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression(v=vs.100).aspx to find out what can be placed in the Expression attribute and how it is evaluated.
Then loDataTable.Columns.Add(loDataColumn); adds the loDataColumn column to the loDataColumn table.
Then the row is added to the table with a personalized column with the Expression attribute, which is executed through loDataTable.Rows.Add(0); . When we add this row, the “Eval” column cell of the loDataTable table is automatically populated with its “Expression” attribute and, if it has operators and SQL queries, etc., it is evaluated and then stored in the cell, so “magic” happens here ", the line with the operators is evaluated and stored in the cell ...
Finally, just return the value stored in the "Eval" column cell in row 0 (this is the index, starts counting from zero) and convert to double using return (double) (loDataTable.Rows[0]["Eval"]); .
And all ... the work is done!
And here is the eaiser code to understand that it is doing the same thing ... This is not inside the method, and it was also explained.
DataTable MyTable = new DataTable(); DataColumn MyColumn = new DataColumn(); MyColumn.ColumnName = "MyColumn"; MyColumn.Expression = "5+5/5" MyColumn.DataType = typeof(double); MyTable.Columns.Add(MyColumn); DataRow MyRow = MyTable.NewRow(); MyTable.Rows.Add(MyRow); return (double)(MyTable.Rows[0]["MyColumn"]);
First create a table using DataTable MyTable = new DataTable();
Then a column with DataColumn MyColumn = new DataColumn();
Then we put the name in the column. Thus, we can search for content in it when it is stored in a table. Done via MyColumn.ColumnName = "MyColumn";
Then, the expression, here we can put a variable of type string, in this case there is a predefined string "5 + 5/5", the result of which is 6.
The type of data that you want to save in the MyColumn.DataType = typeof(double); column MyColumn.DataType = typeof(double);
Add a column to the table ... MyTable.Columns.Add(MyColumn);
Make a row to be inserted into the table that copies the structure of the table DataRow MyRow = MyTable.NewRow();
Add a row to the table using MyTable.Rows.Add(MyRow);
And return the cell value in row 0 of the MyColumn column of the MyColumn table with return (double)(MyTable.Rows[0]["MyColumn"]);
Lesson done !!!