Click here to Skip to main content
15,904,935 members
Please Sign up or sign in to vote.
1.00/5 (5 votes)
See more:
Dear sir i have a datatable with component and farmula and i have Total

Total=12000
Component     Farmula
   CA          800Rs
   IN          50%Rest
   Basic       40%Total
   HRA         40%Basic
   SA          50%Rest


now i want all component value Like
and
calculation is
Rest=Total-(CA+Basic+HRA)=12000-(800+4800+1920)=4480

CA= 800RS = 800
IN= 2240 =50%Rest = 50%4480
Basic= 4800 =40%Total = 40%12000
HRA= 1920 =40%Basic = 40%4800
Sa= 2240 = 50%Rest = 50%4480
Posted
Updated 25-May-15 20:10pm
v4
Comments
Sergey Alexandrovich Kryukov 25-May-15 11:09am    
You need to explain what is supposed to be code and what's input data. Are you going to create an expression during runtime and be able to evaluate it with different input? This is quite possible, but you need to explain where your "dynamic" is.
—SA
[no name] 25-May-15 11:53am    
Do you mean the data in DataTable or SQL Table?
Maciej Los 25-May-15 17:17pm    
Your requirements are not clear, especially the way you count IN and SA values. Please, use "Improve question" widget to update your question.
Abhipal Singh 26-May-15 1:30am    
I can understand your calculations. However, the million dollar question is.. where are you stuck?

Please answer SA's comment
maneesh katiyar 26-May-15 1:34am    
Sir all the component and their's farmulas are dynamic.Means they may change for next time. So i am confused to what approach i use to replace all variable from farmula by Total.

1) I would suggest you to change your approach of having one table per user. This design is not scaleable. What you can do is, have one table with columns UserId, Component and Formula. By doing this you can filter out data based on user ids and get the component and Formula for an individual user.

C#
UserId Component Formula
1  	    CA        800Rs
1  	    IN        50%Rest
1  	    Basic	  40%Total
1  	    HRA       40%Basic
1  	    SA	      50%Rest
2  	    CA        900Rs
2  	    IN        10%Rest
2  	    Basic	  30%Total
2  	    HRA	      20%Basic
2  	    SA	      20%Rest


2) It is possible to dynamically evaluate your expressions. check the links below:
.Net Expression Evaluator using DynamicMethod[^]
Evaluate C# Code (Eval Function)[^]
You need to fetch the formulas from database and then feed them to your custom evaluator as described in links above.
 
Share this answer
 
C#
string Rest = "";
                for (int j = 0; j < dsAllFarmula.Tables[0].Rows.Count; j++)
                {
                   Component cmp = new Component();
                    cmp.ComponentName=dsAllFarmula.Tables[0].Rows[j]["SalaryComponent"].ToString();
                    cmp.CmponentValue = dsAllFarmula.Tables[0].Rows[j]["Farmula"].ToString();
                    Structure.Add(cmp);
                    if (!cmp.CmponentValue.Contains("Rest"))
                        Rest += "("+cmp.ComponentName+")+";
                   // double val= _model.Evaluate(cmp.CmponentValue);
                }
                Rest = Rest.Remove(Rest.Length-1);
                Rest = "Total-(" + Rest+")";
                foreach (var lom in Structure)
                {
                    Rest = Rest.Replace(lom.ComponentName, lom.CmponentValue);
                }

                foreach (var item in Structure)
                {
                    item.CmponentValue = item.CmponentValue.Replace("Rest", Rest);
                    foreach (var pre in Structure)
                        item.CmponentValue = item.CmponentValue.Replace(pre.ComponentName, pre.CmponentValue);

                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900