C# Parse方法引发异常

C# Parse方法引发异常,c#,linq,dynamic-linq,C#,Linq,Dynamic Linq,我正在尝试使用System.Linq.Dynamic库中提供的解析方法。当我执行下面的简单示例时 using Microsoft.Data.SqlClient; using Microsoft.EntityFrameworkCore; using System; using System.Collections.Generic; using System.Linq; using System.Linq.Dynamic; using System.Linq.Expressions; namesp

我正在尝试使用System.Linq.Dynamic库中提供的解析方法。当我执行下面的简单示例时

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);
        }
    }
}

知道我做错了什么吗?

您可以在.net核心项目中使用
System.Linq.Dynamic

Tyr将卸载它,并改为使用
System.Linq.Dynamic.Core

下面是的核心版本的示例

还提供了使用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);
        }
    }
}


什么是InnerException?FileNotFoundException:无法加载文件或程序集“System.Data.Entity,Version=4.0.0.0,Culture=neutral,PublicKeyToken=b77a5c561934e089”。系统找不到指定的文件。您发布的代码没有使用system.Data.Entity中的任何内容,但我注意到您使用了Microsoft.Data.SqlClient和Microsoft.EntityFrameworkCore的
语句。您发布的示例代码实际上并没有再现您报告的异常。你能发布这样的代码吗?@StriplingWarrior添加了一个代码截图,我使用的代码与我在.NET Framework项目中发布的代码相同。解析方法在DynamicExpressionParsery中不可用。您的项目是.net core!!。我可以肯定地说,因为我可以看到
使用Microsoft.EntityFrameworkCore。我用一个完整的例子更新了我的答案。请删除
System.Linq.Dynamic
并添加
System.Linq.Dynamic.Core
您提供了一个ParseLambda方法的示例,但我需要一个Parse方法的示例。@MuhammadWaqas在这两种情况下您都得到了一个
LambdaExpression e
是否需要使用
表达式体
?@MuhammadWaqas我添加了一个新示例。
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);
        }
    }
}


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);
        }
    }
}