To use linq with Rhino it is necessary to modify the source code of linq.js to the conditions of Rhino:
- Rhino does not support variable declaration with
let
andconst
, therefore these must be replaced byvar
. - Rhino does not support
export
declaration, therefore the lineexport default Enumerable;
at the end must be deleted. - Rhino has no console window, therefore it is recommended to replace the code sequences
if (typeof console !== Types.Undefined) { console.log(...); }
withjava.lang.System.out.println(...)
.
You can find a suitably modified version of linq.js as linq.class.js here. With the load command you can use it inside Rhino JavaScript.
A very detailed example is available here, this shows how to use it.
- Rhino supports arrow functions since version 1.7.8. If an older version is used, only regular functions can be used.
// Begin----------------------------------------------------------------
load("linq.class.js");
var Enumerable = LINQ();
// Arrow function expression aka arrow function
var resultArrow = Enumerable.range(1, 10)
.where((i) => i % 3 == 0)
.select((i) => i * 10);
java.lang.System.out.println(
JSON.stringify(resultArrow.toArray())
); // [30,60,90]
// Function expression aka regular function
var resultFunction = Enumerable.range(1, 10)
.where( function(i) { return i % 3 == 0 } )
.select( function(i) { return i * 10 } );
java.lang.System.out.println(
JSON.stringify(resultFunction.toArray())
); // [30,60,90]
// End------------------------------------------------------------------
- linq supports only lambda syntax (dot notation) and not query expression.
// Begin----------------------------------------------------------------
load("linq.class.js");
var Enumerable = LINQ();
var range = Enumerable.range(1, 10);
// Lambda syntax
var result = range
.where( function(i) { return i % 3 == 0 } )
.select( function(i) { return i * 10 } );
/* Query expression is not supported
var result = from i in range where i % 3 == 0 select i * 10;
*/
java.lang.System.out.println(
JSON.stringify(result.toArray())
); // [30,60,90]
// End------------------------------------------------------------------