zzzprojects / Eval-Expression.NET

C# Eval Expression | Evaluate, Compile, and Execute C# code and expression at runtime.
https://eval-expression.net/
Other
449 stars 86 forks source link

Linq Max Min Not Work #146

Closed vzsoft closed 1 year ago

vzsoft commented 1 year ago

Hi,

I got Error in Max Min

my class:

    public class AppUser 
    {
        public string Name { get; set; }
        public int Age { get; set; }
    }

my code:


            var lst = new List<AppUser>() { new AppUser() { Age=20 }, new AppUser() { Age=22 } };

            var test1 = lst.Where(x=> x.Age== 20).Max(x=> x?.Age);

            var testMax =   Eval.Execute("{0}.Where(x => x.Age== 20).Max(x => x?.Age)", lst ); //got Error
            var testMin =   Eval.Execute("{0}.Where(x => x.Age== 20).Min(x => x?.Age)", lst ); //got Error

Thank you

JonathanMagnan commented 1 year ago

Hello @vzsoft ,

Thank you for reporting.

We will look at this. We support the operator ?. but it looks like this causes us an error when using with the Min / Max method.

I believe our library doesn't know which method to take between this 2:

public static int? Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
public static int Max<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);

We will look at it.

Best Regards,

Jon

vzsoft commented 1 year ago

Hi @JonathanMagnan ,

Sum method is like the Max method, but it work


var testSum =   Eval.Execute("{0}.Where(x => x.Age== 20).Sum(x => x?.Age)", lst ); //work

public static int? Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int?> selector);
public static int Sum<TSource>(this IEnumerable<TSource> source, Func<TSource, int> selector);

Thank you

JonathanMagnan commented 1 year ago

Hello @vzsoft ,

Just to let you know that this issue has been fixed. It will be released the next time we release this library (in 1 or 2 weeks).

The method conflict was actually caused by another Max method with more generic parameters. So that explains why the Sum didn't have the issue as it didn't have the other method.

Best Regards,

Jon

vzsoft commented 1 year ago

Thank you so much

JonathanMagnan commented 1 year ago

Hello @vzsoft ,

The fix has finally been released in v5.0.8

Our method resolution logic has been improved with this issue by taking into account the number of generic parameters of a method when trying to find the BetterMember methods.

Let us know if everything works as expected.

Best Regards,

Jon

vzsoft commented 1 year ago

Nice Work, Thank you so much