2011-09-21 154 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的財產。