Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linq Select throws exception (ValueTuple?) issue? #139

Open
yggdrasil-tynor opened this issue Jun 4, 2022 · 3 comments
Open

Linq Select throws exception (ValueTuple?) issue? #139

yggdrasil-tynor opened this issue Jun 4, 2022 · 3 comments
Assignees

Comments

@yggdrasil-tynor
Copy link

context.AddVariable("list1", new int[] { 1, 2, 3, 8 });
context.AddVariable("list2", new int[] { 4, 5, 6, 7 });
var test = context.Evaluate("list1.Cast<int>().Select<int, int>((item, i) => item - list1[i]).Cast<int>().Sum()");

throws:

ExpressionEvaluatorSyntaxErrorException : The call of the method "Sum" on type [System.Linq.Enumerable+SelectArrayIterator`2[System.Int32,System.Int32]] generate this error : Index was outside the bounds of the array.

With Zip or EquiZip it works fine:

list1.Cast<int>().EquiZip<int, int, int>(list2, (l1, l2) => l1 + l2).Cast<int>().Sum()"

What is wrong with select?

@yggdrasil-tynor
Copy link
Author

yggdrasil-tynor commented Jun 7, 2022

EDIT: Seems like it's an issue when using ValueTuple.... Maybe this is not supported?

Here is a simple .NET fiddle example which shows the unexpected behaviour:

@yggdrasil-tynor yggdrasil-tynor changed the title Linq Select throws exception? Linq Select throws exception (ValueTuple?) issue? Jun 7, 2022
@yggdrasil-tynor
Copy link
Author

yggdrasil-tynor commented Jun 7, 2022

This line breaks it:

For some reason the internalDelegate args contains 1 item, and should contain 2 in this case...

// For Linq methods that are overloaded and implement possibly lambda arguments
try
{
if (methodInfos.Count > 1
&& type == typeof(Enumerable)
&& args.Count == 2
&& args[1] is InternalDelegate internalDelegate
&& args[0] is IEnumerable enumerable
&& enumerable.GetEnumerator() is IEnumerator enumerator
&& enumerator.MoveNext()
&& methodInfos.Any(m => m.GetParameters().Any(p => p.ParameterType.Name.StartsWith("Func"))))
{
Type lambdaResultType = internalDelegate.Invoke(enumerator.Current).GetType();

I guess this overload requires some special handling?

@yggdrasil-tynor
Copy link
Author

FYI this is still an issue in 1.5.0-alpha releases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants