C# DataTable动态计算列
我需要一种基于其他字符串列中的字符串更新计算数据表的列的方法。 例如: x y FormulaCol ComputedCol ------------------------------ ----------- 2 5 x+y 7 2 5 x*y 10 x y公式计算公式 ------------------------------ ----------- 2 5 x+y 7 2.5x*y 10 我知道我可以使用for循环并计算结果列:C# DataTable动态计算列,c#,datatable,calculated-columns,C#,Datatable,Calculated Columns,我需要一种基于其他字符串列中的字符串更新计算数据表的列的方法。 例如: x y FormulaCol ComputedCol ------------------------------ ----------- 2 5 x+y 7 2 5 x*y 10 x y公式计算公式 ---------------
for (int i = 0; i < DT.Rows.Count; i++){
string formula=DT.Rows[i]["FormulaCol"].ToString().Replace("x",DT.Rows[i]["x"]).Replace("y",DT.Rows[i]["y"])
DT.Rows[i]["ComputedCol"] =(int)DT.Compute(formula , "")
}
for(int i=0;i
有没有更好的办法?一个简单而漫长的解决方案:
这就是你的桌子的样子 x y FormulaCol ComputedCol ------------------------------ ----------- 2 5 + 7 2 5 * 10 x y公式计算公式 ------------------------------ ----------- 2 5 + 7 2 5 * 10 以及您的代码:
for (int i = 0; i < DT.Rows.Count; i++){
switch(DT.Rows[i]["FormulaCol"].ToString()){
case "+":
int formula=(int) DT.Rows[i]["x"] + (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "-":
int formula=(int) DT.Rows[i]["x"] - (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "*":
int formula=(int) DT.Rows[i]["x"] * (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "/":
int formula=(int) DT.Rows[i]["x"] / (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
}
}
for(int i=0;i
希望这有帮助 一个简单而漫长的解决方案:
这就是你的桌子的样子 x y FormulaCol ComputedCol ------------------------------ ----------- 2 5 + 7 2 5 * 10 x y公式计算公式 ------------------------------ ----------- 2 5 + 7 2 5 * 10 以及您的代码:
for (int i = 0; i < DT.Rows.Count; i++){
switch(DT.Rows[i]["FormulaCol"].ToString()){
case "+":
int formula=(int) DT.Rows[i]["x"] + (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "-":
int formula=(int) DT.Rows[i]["x"] - (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "*":
int formula=(int) DT.Rows[i]["x"] * (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
case "/":
int formula=(int) DT.Rows[i]["x"] / (int) DT.Rows[i]["y"];
DT.Rows[i]["ComputedCol"] = formula;
break;
}
}
for(int i=0;i
希望这有帮助 如果您不想使用循环,请尝试此
DT = DT.AsEnumerable()
.Select(
row =>
{
row["ComputedCol"] = (int)DT.Compute(row["FormulaCol"].ToString()
.Replace("x", row["x"].ToString())
.Replace("y", row["y"].ToString()), "");
return row;
}
).CopyToDataTable<DataRow>();
DT=DT.AsEnumerable()
.选择(
行=>
{
行[“计算列”]=(int)DT.Compute(行[“公式列”].ToString()
.Replace(“x”,第[“x”]”行。ToString()
.替换(“y”,第[“y”行]。ToString(),“”);
返回行;
}
).CopyToDataTable();
如果不想使用循环,请尝试此
DT = DT.AsEnumerable()
.Select(
row =>
{
row["ComputedCol"] = (int)DT.Compute(row["FormulaCol"].ToString()
.Replace("x", row["x"].ToString())
.Replace("y", row["y"].ToString()), "");
return row;
}
).CopyToDataTable<DataRow>();
DT=DT.AsEnumerable()
.选择(
行=>
{
行[“计算列”]=(int)DT.Compute(行[“公式列”].ToString()
.Replace(“x”,第[“x”]”行。ToString()
.替换(“y”,第[“y”行]。ToString(),“”);
返回行;
}
).CopyToDataTable();
用于(int i=0;i
用于(int i=0;i
哪种方式更好?不带for循环的自动计算方式,或带for循环的不带replace的自动计算方式。哪种方式更好?不带for循环的自动计算方式,或带for循环的不带replace的自动计算方式。我只对示例使用x+y,我希望有一个用户可以输入表达式的公式列。我只对示例使用x+y,我希望有一个用户可以输入表达式的公式列。