Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6916663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:37:54+00:00 2026-05-27T09:37:54+00:00

I want to store combination of method invocations and formulas as text or script

  • 0

I want to store combination of method invocations and formulas as text or script into database. for example, i want to store something like this in Database as a string and execute it somewhere in code:

if(Vessel.Weight>200) 
{
    return Cargo.Weight*Cargo.Tariff*2
} 
else 
{
    Cargo.Weight*Cargo.Tariff
} 

invocation:

var cost = executeFormula("CalculateTariff",new {Cargo= GetCargo(), Vessel=GetVessel()});

because these rules will change frequently and i don’t want to deploy dll (CLR solution), and i don’t want to store these rules as SP and mix business rules with DAL.

Any idea or tool?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T09:37:55+00:00Added an answer on May 27, 2026 at 9:37 am

    If you place all values in a hashtable or a dictionary by changing the . with a _ (Vessel.Weight will become “Vessel_Weight”) and simplify the syntax to one line it will be much easier to create a solution. This rule can be written for example as:

    result=(Vessel_Weight>200)
        ?(Cargo_Weight*Cargo_Tariff*2)
        :(Cargo_Weight*Cargo_Tariff)
    

    Having rules defined like the above one you can use the following (draft, not optimal …) code as a guide for a properly coded function that will do the job. I repeat that the following code is not perfect, but bottom line it’s more than enough as a proof of concept.

    Dictionary<string, dynamic> compute = new Dictionary<string, dynamic>();
    compute.Add("Vessel_Weight", 123);
    compute.Add("Cargo_Weight", 24);
    compute.Add("Cargo_Tariff", 9);
    
        string rule = "result=(Vessel_Weight>200)
            ?(Cargo_Weight*Cargo_Tariff*2)
            :(Cargo_Weight*Cargo_Tariff)";
    
        string process = rule.Replace(" ", "");
        foreach (Match level1 in Regex.Matches(process, "\\([^\\)]+\\)"))
        {
            string parenthesis = level1.Value;
            string keepit = parenthesis;
            Console.Write("{0} -> ", parenthesis);
            // replace all named variable with values from the dictionary
            foreach (Match level2 in Regex.Matches(parenthesis, "[a-zA-z0-9_]+"))
            {
                string variable = level2.Value;
                if (Regex.IsMatch(variable, "[a-zA-z_]+"))
                {
                    if (!compute.ContainsKey(variable))
                        throw new Exception("Variable not found");
                    parenthesis = parenthesis.Replace(variable, compute[variable].ToString());
                }
            }
            parenthesis = parenthesis.Replace("(", "").Replace(")", "");
            Console.Write("{0} -> ", parenthesis);
            // do the math
            List<double> d = new List<double>();
            foreach (Match level3 in Regex.Matches(parenthesis, "[0-9]+(\\.[0-9]+)?"))
            {
                d.Add(double.Parse(level3.Value));
                parenthesis = Regex.Replace(parenthesis, level3.Value, "");
            }
            double start = d[0];
            for (var i = 1; i < d.Count; i++)
            {
                switch (parenthesis[i - 1])
                {
                    case '+':
                        start += d[i];
                        break;
                    case '-':
                        start -= d[i];
                        break;
                    case '*':
                        start *= d[i];
                        break;
                    case '/':
                        start /= d[i];
                        break;
                    case '=':
                        start = (start == d[i]) ? 0 : 1;
                        break;
                    case '>':
                        start = (start > d[i]) ? 0 : 1;
                        break;
                    case '<':
                        start = (start < d[i]) ? 0 : 1;
                        break;
                }
            }
            parenthesis = start.ToString();
            Console.WriteLine(parenthesis);
            rule = rule.Replace(keepit, parenthesis);
        }
        Console.WriteLine(rule);
        // peek a value in case of a condition
        string condition = "[0-9]+(\\.[0-9]+)?\\?[0-9]+(\\.[0-9]+)?:[0-9]+(\\.[0-9]+)?";
        if (Regex.IsMatch(rule, condition))
        {
            MatchCollection m = Regex.Matches(rule, "[0-9]+(\\.[0-9]+)?");
            int check = int.Parse(m[0].Value) + 1;
            rule = rule.Replace(Regex.Match(rule, condition).Value, m[check].Value);
        }
        Console.WriteLine(rule);
        // final touch
        int equal = rule.IndexOf("=");
        compute.Add(rule.Substring(0, equal - 1), double.Parse(rule.Substring(equal + 1)));
    

    Now the result is a named item in the dictionary. This way you may process more rules in the sense of intermediate results and have a final rule based on them. The code as is written does not guarantee correct execution order for arithmetic operations, but if you keep your rules simple (and possibly split them if is needed) your will achieve your goal.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to store a large number of sound files in a database, but
I want to store the data returned by $_SERVER[REMOTE_ADDR] in PHP into a DB
I want to store a large result set from database in memory. Every record
I want to store a string in memory and read it later: $$->desc.constant->base.id =
I want to store the current URL in a session variable to reference the
I want to store a a c# DateTimeOffset value in a SQL Server 2005
I want to store values in a bunch of currencies and I'm not too
I want to store a very large amount of vector data on a server
I want to store a URL prefix in an Windows environment variable. The ampersands
I want to store a list of the following tuples in a compressed format

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.