microsoft / ClearScript

A library for adding scripting to .NET applications. Supports V8 (Windows, Linux, macOS) and JScript/VBScript (Windows).
https://microsoft.github.io/ClearScript/
MIT License
1.77k stars 148 forks source link

How to use Linq in microsoft.clearscript.v8 #551

Closed NamanGajjar26 closed 11 months ago

NamanGajjar26 commented 11 months ago

in this case I am use microsoft.clearscript.v8 in asp .net core mvc this is below homecontroller.cs code

` public async Task<IActionResult> Index()
    {

        using (var engine1 = new V8ScriptEngine())
        {

            engine1.AddHostType(typeof(JavaScriptExtensions));

            engine1.DocumentSettings.AccessFlags = DocumentAccessFlags.EnableFileLoading;

            string scriptPath = Path.Combine(Directory.GetCurrentDirectory(), "wwwroot", "js", "NewScript.js");

            string scriptContent = System.IO.File.ReadAllText(scriptPath);

            engine1.Execute(scriptContent);

            dynamic data = engine1.Evaluate("NewLinq();");

            return View();

        }
}`

and this below code in NewScript.js (JavaScript file) function

`function NewLinq() {

const rawData = [
    { "id": 1, "name": "John", "age": 25 },
    { "id": 2, "name": "Jane", "age": 30 }      
];

var result = rawData
    .filter(person => person.age > 25);  

return result;

}`

in this code return UnderlyingSystemType = '((Microsoft.ClearScript.ScriptItem)data).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException' this error how to get output and use linq

ClearScriptLib commented 11 months ago

Hello @NamanGajjar26,

As with your previous sample, we tested your code and encountered no issues. Also, you aren't using Linq anywhere above, so we're not clear on how it's related.

in this code return UnderlyingSystemType = '((Microsoft.ClearScript.ScriptItem)data).UnderlyingSystemType' threw an exception of type 'System.NotImplementedException'

Again, where are you seeing this exception? How can we reproduce it? Can you provide a stack trace?

Thanks!

NamanGajjar26 commented 11 months ago

how to use import in this code in js file

NamanGajjar26 commented 11 months ago

import * as jsonpath from 'D:/VB_Programs/V8JavaJsonModify/V8JavaJsonModify/wwwroot/js/jsonpath.min.js'; function Example1() { var cities = [ { name: "London", "population": 8615246 }, { name: "Berlin", "population": 3517424 }, { name: "Madrid", "population": 3165235 }, { name: "Rome", "population": 2870528 } ];
const result = jsonpath({ path: '$..name', cities }); return result; }

ClearScriptLib commented 11 months ago

how to use import in this code in js file

Please see here.

ClearScriptLib commented 11 months ago

Please reopen this issue if you have additional questions or comments on this topic. Thanks!