question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Sum of expression lamba on List

See original GitHub issue

Hi

I was trying to make an evaluation, a Sum on a List, but I’m getting an error: CodingSeb.ExpressionEvaluator.ExpressionEvaluatorSyntaxErrorException: ‘Function [Sum] unknown in expression : [Sum(x => x.Nombre == “uno” ? (x.Valor ?? 0m) : 0m)]’

What am I doing wrong? or it is not supported?

var Lista = new List<Test>() { new Test { Nombre = "uno", Valor = 1.11m }, new Test { Nombre = "uno", Valor = 2.22m }, new Test { Nombre = "uno", Valor = null }, new Test { Nombre = "dos", Valor = 3.33m } };
string script = "Sum(x => x.Nombre == \"uno\" ? (x.Valor ?? 0m) : 0m)";

ExpressionEvaluator Eval = new ExpressionEvaluator()
{
      StaticTypesForExtensionsMethods = new List<Type> { typeof(Enumerable) },
      Namespaces = new List<string>() { "System", "System.Linq"},
      OptionVariableAssignationActive = false, 
      OptionPropertyOrFieldSetActive = false, 
      CacheTypesResolutions = true
};

Eval.Context = Lista;

var result = Eval.Evaluate(script);

// ***********************************************************************
public class Test
{
     public string Nombre { get; set; }
     public decimal? Valor { get; set; }
}

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
TheoVCcommented, Jul 1, 2021

Hi

Yes I tested and it’s working perfectly fine. it’s the perfect solution for indexing… and for calling context methods…

var Lista = new List<Person>() { new Person { Name = "uno", Value = 1.1m }, new Person { Name = "dos", Value = 2.2m } };

var Eval = new ExpressionEvaluator();
Eval.Context = Lista;
var temp = Eval.Evaluate("10.5m + this.Sum(x => x.Value) + this[0].Value");

Actually this code is in production already… 😃

1reaction
codingsebcommented, Jun 29, 2021

new version 1.4.30.0 introduce the this keyword to manage extention methods and indexing on Context. Thanks @TheoVC.

Read more comments on GitHub >

github_iconTop Results From Across the Web

python - Finding a sum in nested list using a lambda function
One approach is to use a generator expression: total = sum(int(v) for name,v in table).
Read more >
Python Sum Lambda [3 different ways]
Lambda to get sum of a list using reduce. We can leverage the above lambda to get the sum of two elements to...
Read more >
Python program to find the sum of elements of a list using ...
Here, we will write a Python program to find the sum of elements of a list using lambda function. ... Lambda functions in...
Read more >
Python Lambda Functions with Examples | H2kinfosys Blog
The given below is the sum lambda function sum = lambda a, b: a + b ... The filter function takes a function...
Read more >
Python: Calculate the sum of the positive and negative ...
Write a Python program to calculate the sum of the positive and negative numbers of a given list of numbers using the lambda...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found