System.Linq.Dynamic.Core icon indicating copy to clipboard operation
System.Linq.Dynamic.Core copied to clipboard

DynamicExpressionParser.ParseLambda cannot parse a Method which accepts an expression as parameter

Open wertzui opened this issue 5 years ago • 4 comments

I have a Method that accepts an Expression as Parameter. However the Parser does not recognize q and instead tries to find Foo on it.

The Exception is

System.Linq.Dynamic.Core.Exceptions.ParseException: No property or field 'Foo' exists in type 'QueryStart'

Here is a Test to verify the Problem

    [TestClass]
    public class QueryParserTests
    {
        [TestMethod]
        public void Parse_can_Select()
        {
            // Arrange
            var query = "Start.Select(q => q.Foo).ToListAsync()";

            // Act
            var exp = DynamicExpressionParser.ParseLambda<QueryStart, Task>(ParsingConfig.Default, false, query);

            // Assert no error was thrown
            Assert.IsNotNull(exp);
        }

        private class QueryTest
        {
            public int Foo { get; set; }
        }

        private class QueryStart
        {
            public IMyQuery<QueryTest> Start { get; set; }
        }

        private interface IMyQuery<T>
        {
            IMyQuery<Tnew> MySelect<Tnew>(Expression<Func<T, Tnew>> exp);

            Task<List<T>> ToListAsync();
        }
    }

wertzui avatar Aug 20 '19 13:08 wertzui

Maybe related : https://github.com/StefH/System.Linq.Dynamic.Core/issues/259 ???

StefH avatar Aug 20 '19 16:08 StefH

And why would you like to do this?

StefH avatar Aug 20 '19 19:08 StefH

I would like to be able to give a string like "Orders.Select(o => o.Price).SumAsync()" into a Controller so I'm able to generate some kind of dynamic Report.

What I have found out so far is, that there Is a special case for the IEnumerable Extension Methods and not a generic case to handle any Kind of method invocation. The parser does not recognize q as a symbol and therefore things e.Foo is it.Foo.

wertzui avatar Aug 21 '19 06:08 wertzui

I think I did a more in detail explanation of what the issue really is in:

https://github.com/StefH/System.Linq.Dynamic.Core/issues/289

As @wertzui there seems to be some ad-hoc handling for some methods such as Sum() but there is no proper generic handling of argument type propagation or inference for lambda expressions used as method arguments.

david-garcia-garcia avatar Aug 21 '19 07:08 david-garcia-garcia