2011-09-21 158 views
1

我有一个textarea其中用户可以创建使用下拉列表动态式(为运营商,变量等)这样的:映射变量

basic/workingDays * attendingDays 

其中basicworkingDaysattendingDays被保存的值在数据库中。我想在运行时从数据库映射这些变量。我怎样才能做到这一点。

+0

检查了这一点http://social.msdn.microsoft.com/Forums/eu/csharplanguage/thread/f92d53b5-1bba-424a-8991-7e9e54787c23 – Waqas

回答

3

NCalc是一个非常强大的框架,你可以尝试。他们让你定义动态参数,听起来像你所需要的。你可以做这样的事情:

var e = new Expression("basic/workingDays * attendingDays); 

//Set up a custom delegate so NCalc will ask you for a parameter's value 
// when it first comes across a variable 
e.EvaluateParameter += delegate(string name, ParameterArgs args) 
{ 
    if (name == "basic") 
     args.Result = GetBasicValueFromSomeWhere(); 
    else if (/* etc. */) 
    { 
     //.... 
    } 

    //Or if the names match up you might be able to something like: 
    args.Result = dataRow[name]; 
}; 

var result = e.Evaluate(); 

也有一些相关的问题在那里,如this onethis one,给一些其他的选择。

+1

这个确切做什么,我需要,但我不能使用if和else使用NCalc.eg进行阻塞 if(workingdays> 15) (basic + 1000)/ workingDays * attendingDays – John

+1

NCalc确实有一个内置的if函数。语法与C#中的语法略有不同。你做的事情是这样的:'if(someCondition,value true,false when false)' –

+0

我试图做同样的事情,但它抛出异常(在','在线1:10丢失EOF) 给定的条件是: var e = new Expression(“if(10> 11),10,12”); – John

1

您也可以尝试在DataTable中利用ExpressionDataColumn的财产。