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

Can't parse Select Linq with index parameter => Func<TSource, int, TResult>

Open David-Moreira opened this issue 4 years ago • 6 comments

Hello @StefH ,

I can't seem to get this one to parse. Could you help?

var TestExpression
    = "Input.Lista.Where(x => x.Name != \"\").Select((item, index) => (item.Name.ToString(), index))";

ParameterExpression input = System.Linq.Expressions.Expression.Parameter(typeof(TemplateInput), "Input");
var result = System.Linq.Dynamic.Core.DynamicExpressionParser
    .ParseLambda(new ParameterExpression[] { input }, null, TestExpression);
var lambdaCompile = result.Compile();

Basically trying to use this Select Signature: public static IEnumerable<TResult> Select<TSource, TResult>(this IEnumerable<TSource> source, Func<TSource, int, TResult> selector);

image

David-Moreira avatar Apr 14 '21 10:04 David-Moreira

Only the 'normal' Select is supported -> https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/Parser/SupportedMethods/IQueryableSignatures.cs#L43

I'll take a look if in some way, your request can be implemented easily.

StefH avatar Apr 14 '21 19:04 StefH

@StefH

Only the 'normal' Select is supported

Is this because in general Dynamic LINQ only supports lambda expressions with a single parameter? Or because this particular signature extends only on IEnumerable and not on IQueryable?

zspitz avatar Apr 14 '21 19:04 zspitz

Only the 'normal' Select is supported -> https://github.com/zzzprojects/System.Linq.Dynamic.Core/blob/master/src/System.Linq.Dynamic.Core/Parser/SupportedMethods/IQueryableSignatures.cs#L43

I'll take a look if in some way, your request can be implemented easily.

I see thanks!

David-Moreira avatar Apr 14 '21 20:04 David-Moreira

@stefH meanwhile do you know a way I could get the indexes of the elements in the collection that satisfies a certain condition using dynamic linq? Thanks again!

David-Moreira avatar Apr 14 '21 20:04 David-Moreira

@zspitz : correct, only single lambda are supported. I'm looking at the code to see if this can be extended, however it seems diffucult.

@David-Moreira : not that I know, sorry

StefH avatar Apr 16 '21 19:04 StefH

@StefH I understand, if it's possible, that'd be awesome. Both Where and Select have that second parameter just as the both those extensions in System.Linq

Thank you for your time! 👍

David-Moreira avatar Apr 17 '21 13:04 David-Moreira