Linq Dynamic ParseLambda does not allow

I'm trying to use the sample code that I found here for something I'm working on: How to convert a string to an equivalent LINQ expression tree?

In the decision, the author uses the following:

var e = DynamicExpression.ParseLambda(new[] { p }, null, exp); 

However, when I try to use it, it is not allowed. I get an error message:

System.Linq.Expressions.DynamicExpression 'does not contain a definition for' ParseLambda '

I installed the System Linq Dynamic nuget package in the project, I also added the using statement:

 using System.Linq.Dynamic; 

However, it looks gray, so I guess this does not mean that the DynamicExpression object that I am accessing from there, it selects it from System.Linq.Expression. Is there any way to fix this? I tried to do it

 System.Linq.Dynamic.ParseLambda(new[] { p }, null, tagCondition); 

but still no good, same error, and the using statement is still inactive.

+5
source share
2 answers

To solve this problem, I ended up looking for the Dynamic.cs file: https://msdn.microsoft.com/en-us/vstudio/bb894665.aspx?f=255&MSPPError=-2147217396 I added that I used it to my solution, DynamicExpression was publicly available in this class, so I was found.

0
source

Aggregates

 System.Linq.Dynamic; System.Linq.Expressions; 

both contain DynamicExpression . Since you need both, you need to either specify the alias System.Linq.Dynamic , or explicitly as System.Linq.Dynamic.DynamicExpression

You need to make sure that you have installed System.Linq.Dynamic using the package manager.

Here is a complete minimally working example:

 using System.Linq.Expressions; using myAlias = System.Linq.Dynamic; namespace ConsoleApplication11 { public class Foo { public string Bar { get; set; } } class Program { static void Main(string[] args) { var expression = @"(Foo.Bar == ""barbar"")"; var p = Expression.Parameter(typeof(Foo), "Foo"); var e = myAlias.DynamicExpression.ParseLambda(new[] { p }, null, expression); var test1 = new Foo() { Bar = "notbarbar", }; var test2 = new Foo() { Bar = "barbar" }; // false var result1 = e.Compile().DynamicInvoke(test1); // true var result2 = e.Compile().DynamicInvoke(test2); } } } 
+7
source

Source: https://habr.com/ru/post/1233885/


All Articles