-1

This is the conditional rule configured in the database. The rules configured for each user are as follows。

| key  | expression     |rule|
|:---- |:--------------:|---:|
| 001  | >=500 and <=600| 1.2|
| 001  | >600           | 2.0|
| 002  | ==400          | 4.0|
| 002  | !=700          | 5.0|
| 003  | ==100 || ==200 | 0.5|

I need to get the conditional dynamic judgment that the key is 001 Below is my current code, I want generated C# code like this

if (item.TotalDaySam >= 500 && item.TotalDaySam <= 600)
                    {
                        // return Amount * 001 rule(1.2)
                    }
                    else if (item.TotalDaySam > 600)
                    {
                        // return Amount * 001 rule(2.0)
                    }
                    else
                    {
                        // retrun Amount
                    }

How do I get the configuration of the database to dynamically generate the judgment code to perform different logical calculations. I found a similar project RulesEngine, but I don't know how to implement it.

jun chen
  • 31
  • 6
  • Do I understand correctly that you want generated SQL based on what rules are stored in the table you described? Or do you want generated C# (or whatever lang you use) code based on what rules are described in the table? – AndrasCsanyi Jul 26 '22 at 11:44
  • i want generated C# – jun chen Jul 26 '22 at 11:45
  • You either process the string and navigate the code on certain paths (seems pain in the rear and it is error prone), or you generate code dynamically using either the inbuilt C# generator (donno whether it is already released) or any other template engine. The latter includes runtime code generation. – AndrasCsanyi Jul 26 '22 at 11:49
  • I need to dynamically judge and then calculate based on the database configuration. I don't know how to implement this solution, or do you have other solutions and ideas? – jun chen Jul 26 '22 at 11:53
  • If the code is running constantly then you need runtime code generation. Please, search for it. If this process is triggered then when it is triggered it reads out the filter conditions and generates all the stuff. It is not runtime, and easier, but have its drawbacks. IMHO. I have never done such a thing. – AndrasCsanyi Jul 26 '22 at 11:56
  • My project needs to do different calculations based on user configuration rules, I don't know how to implement this function. – jun chen Jul 26 '22 at 12:05
  • [Please do not upload images of code/data/errors when asking a question.](//meta.stackoverflow.com/q/285551) – Heretic Monkey Jul 26 '22 at 12:11

1 Answers1

2

If you can store your data like this :

  • x>=500 && x<=600
  • x>600
  • x==600
  • x!=600

And then iterate foreach line replacing each time x by "item.TotalDaySam".

Finally you can find help from this post to parse the string into a if statement : C# Convert string to if condition

(sorry for the answer instead of comment I am not expert enough to have the right to comment ^^)

LeauHic
  • 54
  • 5