System.Linq.Dynamic.Core
System.Linq.Dynamic.Core copied to clipboard
DynamicExpressionParser.ParseLambda cannot parse a Method which accepts an expression as parameter
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();
}
}
Maybe related : https://github.com/StefH/System.Linq.Dynamic.Core/issues/259 ???
And why would you like to do this?
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
.
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.