Parse method throws an exception

c# dynamic-linq linq

Question

I am trying to use Parse 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");

            Dictionary<string, ParameterExpression> symbols = new Dictionary<string, ParameterExpression>();

            symbols.Add("x", x);
            symbols.Add("y", y);

            Expression body = System.Linq.Dynamic.DynamicExpression.Parse(null, "(x + y) * 2", symbols);

            LambdaExpression e = Expression.Lambda(body, new ParameterExpression[] { x, y });

            var c = e.Compile();
            var result = c.DynamicInvoke(1, 2);

            Console.WriteLine(result);
        }
    }
}

it throws the following exception.

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

InnerException
FileNotFoundException: Could not load file or assembly 'System.Data.Entity, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.

enter image description here Any idea what I am doing wrong?

1
0
4/28/2020 9:16:44 AM

Accepted Answer

You are possible using System.Linq.Dynamic in a .net core project.

Tyr uninstalling that and instead use System.Linq.Dynamic.Core.

Here is an example of the core version of The ParseLambda Methods

using System;
using System.Linq.Dynamic.Core;
using System.Linq.Expressions;

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

            var c = e.Compile();
            var result = c.DynamicInvoke(1, 2);

            Console.WriteLine(result);
        }
    }
}

Also an example using ExpressionParser


using System;
using System.Linq.Dynamic.Core;
using System.Linq.Dynamic.Core.Parser;
using System.Linq.Expressions;


namespace ConsoleApp2
{
    class Program
    {
        static void Main(string[] args)
        {
            ParameterExpression x = Expression.Parameter(typeof(int), "x");
            ParameterExpression y = Expression.Parameter(typeof(int), "y");

            var symbols = new[] { x, y };

            Expression body = new ExpressionParser(symbols, "(x + y) * 2", symbols, new ParsingConfig()).Parse(typeof(int));

            LambdaExpression e = Expression.Lambda(body, new ParameterExpression[] { x, y });

            var c = e.Compile();
            var result = c.DynamicInvoke(1, 2);

            Console.WriteLine(result);
        }
    }
}

1
4/30/2020 12:12:53 AM


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