sebastienros / jint

Javascript Interpreter for .NET
BSD 2-Clause "Simplified" License
4.12k stars 562 forks source link

Error when using LINQ Select an object or GroupBy an Object #1703

Closed xxyuze closed 10 months ago

xxyuze commented 10 months ago

Version used

3.0.0-beta-2057

Describe the bug

I can use linq to select a value, but I can't select an object.

var engine = new Engine(cfg => cfg
    .AllowClr()
    .AddExtensionMethods(typeof(System.Linq.Enumerable))
);
let someArray = [ 2, 4 ];
let newArray = someArray.Select(m => ({a:m,b:m})).ToArray();

The error message "Object must implement IConvertible"

To Reproduce

Ideally code samples or better yet, xUnit test against current main.

Expected behavior

A clear and concise description of what you expected to happen.

Additional context

Add any other context about the problem here.

lahma commented 10 months ago

Why do you want to dramatically slow down the script performance by using LINQ interop where simple Array.map would suffice?

xxyuze commented 10 months ago

The above is just an example to show that although Jint already supports LINQ operations, it seems incomplete. #304 Sometimes you need to operate on .NET objects, and you must use LINQ operations.

In addition, GroupBy will also report the same error.

var engine = new Engine(cfg => cfg
    .AllowClr()
    .AddExtensionMethods(typeof(System.Linq.Enumerable))
);
;
someDotnetList.GroupBy(m => ({a:m,b:m})).ToArray();
lahma commented 10 months ago

Would you like to help out with a fix PR including missing test coverage for the cases you've found?