nreco / lambdaparser

Runtime parser for string expressions (formulas, method calls). Builds dynamic LINQ expression tree and compiles it to lambda delegate.
http://www.nrecosite.com/
MIT License
307 stars 55 forks source link

Linq Functions Suppport #25

Closed minikie closed 4 years ago

minikie commented 4 years ago

Expression of "values.Count" is working. But values.Max or values.Min is not working... can you fix it?

VitaliyMF commented 4 years ago

"Count" is a property of IList interface; there are no "Max" or "Min" properties there. In C# code you can use values.Max() or values.Min() - but this is extension methods (in fact, static methods defined in System.Linq). Static methods cannot be used in LambdaParser.

If you need to calculate Min or Max you can provide these functions into the evaluation context:

varContext["myValues"] = new [] { 1, 5, 10 };
varContext["Min"] = (Func<IList,object>)((values) => {
  // here is your code that returns 'max' value for generic IList input
});
Console.WriteLine(lambdaParser.Eval("Max(myValues)", varContext));
minikie commented 4 years ago

"Count" is a property of IList interface; there are no "Max" or "Min" properties there.

In C# code you can use values.Max() or values.Min() - but this is extension methods (in fact, static methods defined in System.Linq). Static methods cannot be used in LambdaParser.

If you need to calculate Min or Max you can provide these functions into the evaluation context:


varContext["myValues"] = new [] { 1, 5, 10 };

varContext["Min"] = (Func<IList,object>)((values) => {

  // here is your code that returns 'max' value for generic IList input

});

Console.WriteLine(lambdaParser.Eval("Max(myValues)", varContext));

Thanks~!