Search Wiki:
Project Description
LINQ to JavaScript (JSLINQ) is an implementation of LINQ to Objects implemented in JavaScript. It is built using a set of extension methods built on top of the JavaScript Array object. If you are using an Array, you can use LINQ to JavaScript.

View Interactive SDK: http://simplovation.com/jslinqsdk/

This project is maintained by Chris Pietschmann (http://pietschsoft.com), and sponsored by Simplovation http://simplovation.com.

What is LINQ to JavaScript?


LINQ to JavaScript (JSLINQ for short) is an implementation of LINQ to Objects implemented in JavaScript. It is built using a set of extension methods built on top of the JavaScript Array object. If you are using an Array, you can use JSLINQ.

If you don't know what LINQ is; it's a new featureset in the .NET Framework 3.5 that allows more SQL-like querying of any kind of data. In the case of LINQ to JavaScript, it provides the ability to query against Arrays.

Example Usage
var myList = [
            {FirstName:"Chris",LastName:"Pearson"},
            {FirstName:"Kate",LastName:"Johnson"},
            {FirstName:"Josh",LastName:"Sutherland"},
            {FirstName:"John",LastName:"Ronald"},
            {FirstName:"Steve",LastName:"Pinkerton"}
            ];
            
var exampleArray = From(myList).
                   Where("item.FirstName == 'Chris'").
                   OrderBy("item.FirstName").
                   Select("item.FirstName");


Using LINQ to JavaScript

We will use this Array for the following examples:
var myList = [
            {FirstName:"Chris",LastName:"Pearson"},
            {FirstName:"Kate",LastName:"Johnson"},
            {FirstName:"Josh",LastName:"Sutherland"},
            {FirstName:"John",LastName:"Ronald"},
            {FirstName:"Steve",LastName:"Pinkerton"}
            ];


Use the From operator just as you would in LINQ in .NET
The From operator is optional, and only included in LINQ to JavaScript to be able to support the same syntax as LINQ.
var example = From(myList);


Using the Where operator to specify query criteria
In this case, we're getting all items in the Array that have FirstName property set to Chris.
// Send in the clause as a string to be evaluted
var whereExample1 = From(myList).
                      Where("item.FirstName == 'Chris'");
 
// Send in the clause as a method to be evaluated
var whereExample2 = From(myList).
                      Where(function(item){return item.FirstName == 'Chris';});


Using the Select operator to specify which data to return
In this case, we're going to return only the FirstName property of each item in the Array.
// Send in the clause as a string to be evaluted
var selectTest1 = From(myList).
                Select("item.FirstName");
 
// Send in the clause as a method to be evaluted
var selectTest2 = From(myList).
                Select(function(item){return item.FirstName;});


Using the OrderBy operator to determine how to sort the order of the items in the Array
In this case, we're going to order them by the FirstName property.
// Send in the clause as a string to be evaluted
var sortTest1 = From(myList).
             OrderBy("item.FirstName");
 
// Send in the clause as a method to be evaluted
var sortTest1 = From(myList).
             OrderBy(function(item){return item.FirstName});

Last edited Feb 2 at 1:44 AM  by crpietschmann, version 14
Updating...