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

No applicable member has been found for the expression #69

Closed vzsoft closed 4 years ago

vzsoft commented 4 years ago

Hi,

I got Error in Eval.Execute

https://dotnetfiddle.net/T1id3Q

thank you so much

my class:

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

my code:


var lst = Enumerable.Range(1, 10).Select(x => new AppUser() { Name = "1", Age = x }).ToList();

Eval.Execute("({0}).GroupBy(x => x.Name, (x0, y0) => new { value = x0, field = Name, aggregates = new { sum = y0.Sum(g => g?.Age), max = y0.Max(g => g?.Age) } }).ToList()", lst);
vzsoft commented 4 years ago

https://dotnetfiddle.net/T1id3Q

JonathanMagnan commented 4 years ago

Hello @vzsoft ,

The issue is caused by this part: field = Name

That's not valid, the name comes from nowhere.

That's probably not what you want but here is an example that works:

Eval.Execute("({0}).GroupBy(x => x.Name, (x0, y0) => new { value = x0, field = y0.Max(z => z.Name), aggregates = new { sum = y0.Sum(g => g?.Age), max = y0.Max(g => g?.Age) } }).ToList()", lst);

You probably want the name of the field in the group by, here is another example:

ar item = Eval.Execute("({0}).GroupBy(x => x.Name, (x0, y0) => new { value = x0, field = 'Name', aggregates = new { sum = y0.Sum(g => g?.Age), max = y0.Max(g => g?.Age) } }).ToList()", lst);

Let me know if that helps you to get your expressions solved.

Best Regards,

Jon