ParseLambda method throws an exception

c# dynamic-linq linq

Question

I am trying to use ParseLmabda method available in System.Linq.Dynamic library. When I execute the following simple example,

using Microsoft.Data.SqlClient;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Dynamic;
using System.Linq.Expressions;

namespace DynamicLINQDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            ParameterExpression x = Expression.Parameter(typeof(int), "x");
            ParameterExpression y = Expression.Parameter(typeof(int), "y");
            LambdaExpression e = System.Linq.Dynamic.DynamicExpression.ParseLambda(new ParameterExpression[] { x, y }, null, "(x + y) * 2");
        }
    }
}

it throws the following exception.

System.TypeInitializationException: 'The type initializer for 'System.Linq.Dynamic.ExpressionParser' threw an exception.

Any idea what I am doing wrong?

1
0
4/22/2020 8:11:54 AM

Accepted Answer

When using System.Linq.Dynamic.Core, this works:

ParameterExpression x = Expression.Parameter(typeof(int), "x");
ParameterExpression y = Expression.Parameter(typeof(int), "y");
LambdaExpression e = DynamicExpressionParser.ParseLambda(new [] { x, y }, null, "(x + y) * 2");

var c = e.Compile();
var result = c.DynamicInvoke(1, 2);
Console.WriteLine(result);
1
4/24/2020 6:43:13 PM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow