Search code examples
c#linqdynamic-linq

System.Linq.Dynamic.Core.DynamicExpressionParser.ParseLambda throwing System.NotSupportedException


Trying to use ParseLmabda method available in System.Linq.Dynamic.Core library. When I execute the following simple example.

using (var context = new EntityContext())
{
    Expression<Func<Customer, bool>> e1 = System.Linq.Dynamic.Core.DynamicExpressionParser
        .ParseLambda<Customer, bool>(null, true, "City = @0", "London");

    var customers = context.Customers.Where("@0(it)", e1).ToList();

}

it throws the following exception.

System.NotSupportedException: 'The LINQ expression node type 'Invoke' is not supported in LINQ to Entities.'

enter image description here

Any idea what I am doing wrong?


Solution

  • Did you try this code:

    Expression<Func<Customer, bool>> e1 = DynamicExpressionParser.ParseLambda<Customer, bool>(null, true, "City = @0", "London");
    
    var customers = Customers.Where(e1).ToList();
    

    enter image description here

    Update (2020-06-01)

    Actually, code below should just work fine.

    var e1 = DynamicExpressionParser.ParseLambda<Customer, bool>(new ParsingConfig(), true, "City = @0", "London");
    var e2 = DynamicExpressionParser.ParseLambda<Customer, bool>(new ParsingConfig(), true, "c => c.CompanyName != \"test\"");
    
    var customers = context.Customers.ToList().AsQueryable().Where("@0(it) and @1(it)", e1, e2);
    

    See example: https://dotnetfiddle.net/PF9Htk