C# 如何使数据表识别函数

C# 如何使数据表识别函数,c#,asp.net,function,C#,Asp.net,Function,我在网上的某个地方找到了一个可以执行字符串方程的函数。例如,我可以有一个字符串,如“1+5”。当我将其传递给函数时,它返回答案。在我的应用程序中,我从数据库中提取公式,并用用户使用应用程序时输入的实际数字(1、2、3、4、5等)替换未知数字(如x、y或z)。然而,我需要执行的一些公式需要平方根。当我试图传递一个包含Math.Sqrt()函数的字符串时,它会告诉我一个错误,说它是一个未定义的函数。有没有办法修改函数以识别此函数 该函数使用数据表。代码如下: public static double

我在网上的某个地方找到了一个可以执行字符串方程的函数。例如,我可以有一个字符串,如“1+5”。当我将其传递给函数时,它返回答案。在我的应用程序中,我从数据库中提取公式,并用用户使用应用程序时输入的实际数字(1、2、3、4、5等)替换未知数字(如x、y或z)。然而,我需要执行的一些公式需要平方根。当我试图传递一个包含Math.Sqrt()函数的字符串时,它会告诉我一个错误,说它是一个未定义的函数。有没有办法修改函数以识别此函数

该函数使用数据表。代码如下:

public static double executeFormula(string expression)
{
    System.Data.DataTable table = new System.Data.DataTable();
    table.Columns.Add("expression", string.Empty.GetType(), expression);
    System.Data.DataRow row = table.NewRow();
    table.Rows.Add(row);
    return double.Parse((string)row["expression"]);
}  
谢谢大家

您可以创建人们能够识别和使用的“文本函数”,您可以在程序中解析这些函数。看看或者。例如,您可以执行以下操作:

if(str.IndexOf("sqrt")>-1)
{
    //replace sqrt for the actual Math.Sqrt() function
}
祝你好运

您可以创建人们能够识别和使用的“文本函数”,您可以在程序中解析这些函数。看看或者。例如,您可以执行以下操作:

if(str.IndexOf("sqrt")>-1)
{
    //replace sqrt for the actual Math.Sqrt() function
}

祝你好运

恐怕答案是否定的,正如你所写的那样。您显示的函数是利用数据表中DataColumn的一个特性,即属性

实际上,它支持SQL中的许多运算符:

从文件中:

OPERATORS

Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use parentheses to group clauses and force precedence. The AND operator has precedence over other operators. For example:

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'

When creating comparison expressions, the following operators are allowed:

<

>

<=

>=

<>

=

IN

LIKE

The following arithmetic operators are also supported in expressions:

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

STRING OPERATORS

To concatenate a string, use the + character. Whether string comparisons are case-sensitive or not is determined by the value of the DataSet class's CaseSensitive property. However, you can override that value with the DataTable class's CaseSensitive property.

WILDCARD CHARACTERS

Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ([]). If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example:

"ItemName LIKE '*product*'"

"ItemName LIKE '*product'"

"ItemName LIKE 'product*'"

Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed.

PARENT/CHILD RELATION REFERENCING

A parent table may be referenced in an expression by prepending the column name with Parent. For example, the Parent.Price references the parent table's column named Price.

A column in a child table may be referenced in an expression by prepending the column name with Child. However, because child relationships may return multiple rows, you must include the reference to the child column in an aggregate function. For example, Sum(Child.Price) would return the sum of the column named Price in the child table.

If a table has more than one child, the syntax is: Child(RelationName). For example, if a table has two child tables named Customers and Orders, and the DataRelation object is named Customers2Orders, the reference would be:

Avg(Child(Customers2Orders).Quantity)

AGGREGATES

The following aggregate types are supported:

Sum (Sum)

Avg (Average)

Min (Minimum)

Max (Maximum)

Count (Count)

StDev (Statistical standard deviation)

Var (Statistical variance).

Aggregates are usually performed along relationships. Create an aggregate expression by using one of the functions listed above and a child table column as detailed in PARENT/CHILD RELATION REFERENCING above. For example:

Avg(Child.Price)

Avg(Child(Orders2Details).Price)
如果愿意,可以将这种函数放入方法中


或者,您可能希望在其他地方处理公式;您可以使用类,而不是使用DataTable对数据进行建模。类本身可以具有表示您的数据的属性(或列表或集合),以及作用于您的数据的方法-然后您可以在C#中使用您想要的任何函数。

我恐怕答案是否定的,正如您所写的那样。您显示的函数是利用数据表中DataColumn的一个特性,即属性

实际上,它支持SQL中的许多运算符:

从文件中:

OPERATORS

Concatenation is allowed using Boolean AND, OR, and NOT operators. You can use parentheses to group clauses and force precedence. The AND operator has precedence over other operators. For example:

(LastName = 'Smith' OR LastName = 'Jones') AND FirstName = 'John'

When creating comparison expressions, the following operators are allowed:

<

>

<=

>=

<>

=

IN

LIKE

The following arithmetic operators are also supported in expressions:

+ (addition)

- (subtraction)

* (multiplication)

/ (division)

% (modulus)

STRING OPERATORS

To concatenate a string, use the + character. Whether string comparisons are case-sensitive or not is determined by the value of the DataSet class's CaseSensitive property. However, you can override that value with the DataTable class's CaseSensitive property.

WILDCARD CHARACTERS

Both the * and % can be used interchangeably for wildcards in a LIKE comparison. If the string in a LIKE clause contains a * or %, those characters should be escaped in brackets ([]). If a bracket is in the clause, the bracket characters should be escaped in brackets (for example [[] or []]). A wildcard is allowed at the beginning and end of a pattern, or at the end of a pattern, or at the beginning of a pattern. For example:

"ItemName LIKE '*product*'"

"ItemName LIKE '*product'"

"ItemName LIKE 'product*'"

Wildcards are not allowed in the middle of a string. For example, 'te*xt' is not allowed.

PARENT/CHILD RELATION REFERENCING

A parent table may be referenced in an expression by prepending the column name with Parent. For example, the Parent.Price references the parent table's column named Price.

A column in a child table may be referenced in an expression by prepending the column name with Child. However, because child relationships may return multiple rows, you must include the reference to the child column in an aggregate function. For example, Sum(Child.Price) would return the sum of the column named Price in the child table.

If a table has more than one child, the syntax is: Child(RelationName). For example, if a table has two child tables named Customers and Orders, and the DataRelation object is named Customers2Orders, the reference would be:

Avg(Child(Customers2Orders).Quantity)

AGGREGATES

The following aggregate types are supported:

Sum (Sum)

Avg (Average)

Min (Minimum)

Max (Maximum)

Count (Count)

StDev (Statistical standard deviation)

Var (Statistical variance).

Aggregates are usually performed along relationships. Create an aggregate expression by using one of the functions listed above and a child table column as detailed in PARENT/CHILD RELATION REFERENCING above. For example:

Avg(Child.Price)

Avg(Child(Orders2Details).Price)
如果愿意,可以将这种函数放入方法中

或者,您可能希望在其他地方处理公式;您可以使用类,而不是使用DataTable对数据进行建模。类本身可以具有表示您的数据的属性(或列表或集合),以及作用于数据的方法—然后您可以在C#中使用您想要的任何函数。

使用并完成它

它完全支持您正在谈论的内容,并且对于您尚未触及的内容,它是可扩展的

使用并完成它


它完全支持您正在谈论的内容,并且对于您尚未触及的内容,它是可扩展的

看看CodePlex项目。“逃离”允许您在运行时计算字符串表达式,例如
“sqrt(a^2+b^2)”

请查看CodePlex项目。“逃离”允许您在运行时计算字符串表达式,例如
“sqrt(a^2+b^2)”

您使用的“从在线某处”是什么?我不太确定您在问什么。如果你是说我从哪里得到的,我是在这样一个论坛上得到的,回答了一个关于解析字符串方程能力的问题。如果你问我用它做什么,那是为了我在学校的一个项目。我们需要解析从数据库中提取的公式。而不是
string.Empty.GetType()
您可以编写
typeof(string)
。您到底在使用什么“从在线某处”呢?我不太确定您在问什么。如果你是说我从哪里得到的,我是在这样一个论坛上得到的,回答了一个关于解析字符串方程能力的问题。如果你问我用它做什么,那是为了我在学校的一个项目。我们需要解析从数据库中提取的公式。而不是
string.Empty.GetType()
您可以编写
typeof(string)
@dash:它是。我们用它来解决两个主要问题。第一个运行一些直到运行时才知道的复杂数学函数。第二,我们使用它实现了一个简单的页面处理规则引擎。非常简单易用,启动速度非常快。太棒了。有史以来最好的图书馆!非常感谢@达什:是的。我们用它来解决两个主要问题。第一个运行一些直到运行时才知道的复杂数学函数。第二,我们使用它实现了一个简单的页面处理规则引擎。非常简单易用,启动速度非常快。太棒了。有史以来最好的图书馆!非常感谢!