0

I have been looking for any tip or idea about to Compute Enumerables using a String Expression, I mean by compute to execute operations like Min, Max, Average and Sum.

I am looking for something like This..

    // PARAMETER MODEL
    public class Parameter
    {
        public string Name { get; set; }
        public double Price1 { get; set; }
        public double Price2 { get; set; }
        public double Price3 { get; set; }
        public double Price4 { get; set; }
    }

    // GETS AN ENUMERABLE COLLECTION OF PARAMETERS
    IEnumerable<Parameter> currentParameters = ParameterCreator.GetParameters();

    // HERE IS MY QUESTION
    double maxPrice = currentParameters.Compute("MAX[Price2]");

Where Price2 String Expressions indicates a property in Parameter Class. MAX a Compute through Price2 parameter on the Enumerable Collection. And Compute, a dummy extension method on any package or framework.

Does anyone has any idea whether there is any package, framework or anything to achieve this?

Thank you for your help.

  • You'll need to write a parser for your simple language and a visitor to interpret it. You can use something like Sprache for this. However, this smells a lot like an XY problem. Depending on your use case (is the instruction coming from a user, perhaps over the wire? do all "operations" involve reducing a list to a single element?), this can be solved in a number of simpler ways, such as passing delegates or lambda expressions, or a simple switch over an enum. – Asad Saeeduddin Oct 25 '17 at 02:30
  • You could dust off the old [Dynamic LINQ](https://github.com/kahanu/System.Linq.Dynamic/wiki/Dynamic-Expressions) library. – Mike Strobel Oct 25 '17 at 02:32
  • `List` has a max method: `var list = new List { 1, 2, 3 };var max = list.Max(x => x);`. Wouldn't that do? – CodingYoshi Oct 25 '17 at 02:43

0 Answers0