BT

JSINQ, a JavaScript Implementation of LINQ

| by Abel Avram Follow 10 Followers on Feb 05, 2009. Estimated reading time: 1 minute |

JSINQ stands for JavaScript INtegrated Query, a beta project implementing LINQ to Objects in JavaScript and recently released on CodePlex by Kai Jäger. JSINQ contains two modules, Enumerable and Query implementing System.Linq.Enumerable and System.Linq.Queryable respectively, allowing one to enumerate over arrays, DOM node lists or other objects.

The two modules are jsinq.Enumerable and jsinq.Query. An example of a query is:

var query = new jsinq.Query('\
    from order in $1 \
    group order by order.customerId into g \
    select {customerId: g.getKey(), items: g.sum(function(g) { return g.items; })} \
    into h \
    join customer in $0 on h.customerId equals customer.id \
    where h.items > 10 \
    orderby h.items descending \
    select {lastname: customer.lastname, items: h.items} \
');
query.setValue(0, customers);
query.setValue(1, orders);
var result = query.execute();

An example of enumerating is:

var enumerator = namesThatStartWithAnA.getEnumerator();
while (enumerator.moveNext()) {
	var name = enumerator.current();
	document.write(name + '<br  />');
}

According to Kai, JSINQ can do:

  • Write arbitrarily complex queries against JavaScript arrays, DOM node lists or your own enumerable types
  • Find elements in the HTML DOM tree using SQL-like queries
  • Dynamically create HTML elements from JSON you have received via XMLHttpRequest in a declarative manner
  • Tinker with XML and turn it into something else
  • Combine it in interesting ways with the JavaScript-/Ajax-frameworks you are already using
  • Write less code by exploiting the power of declarative programming
  • And for the ambitious: write raytracers, monadic parser combinators, etc.

Kai has put up a playground web page allowing anyone to test JSINQ. The source code can be downloaded for free from CodePlex under the MIT License. Other similar projects: JSLINQ and JLINQ.

Rate this Article

Adoption Stage
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread
Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Discuss

Login to InfoQ to interact with what matters most to you.


Recover your password...

Follow

Follow your favorite topics and editors

Quick overview of most important highlights in the industry and on the site.

Like

More signal, less noise

Build your own feed by choosing topics you want to read about and editors you want to hear from.

Notifications

Stay up-to-date

Set up your notifications and don't miss out on content that matters to you

BT