LInQer ports .NET LINQ to Javascript
Written by Nikos Vaggalis   
Tuesday, 25 February 2020

LinQer is a library that sprang out of the need to fix two problems of Javascript's built-in iteration methods. It brings .NET's LINQ approach to databases to JavaScript.

The problems with the iteration methods that needed fixing were mainly that:

  • they are applicable to arrays only

and that 

they are eager,meaning that the set undergoing intermittent transformations is the full one,even when only a few items are needed from it:

"Every time you filter or you map something you create a new array".

 

LinQer introduces a class called Enumerable, just like its .NET counterpart, which wraps any iterable structure, for example generators and not just arrays, providing the means to work with them under the same interface that .NET's Enumerable supports. For example,a pretty valid operation to an Enumerable instance is the following :

const result = enumerable
   .where(item=>!!item.value) 
   .select(item=>{ value: item.value, key: item.name }) 
   .groupBy(item=>item.key)
   .where(g=>g.length>10)
   .orderBy(g=>g.key)
   .selectMany()
   .skip(15)
   .take(5);

Therefore, apart from the built in map,filter and reduce operations, LinQer extends Javascript with those rich constructs too. Still, not the full spectrum of .NET Emumerable's methods was implemented, due to the mismatch between C# and Javascript; some things just don't make sense under Javascript's context. Hence, toDictionary, toHashSet, toLookup, toList have been left out with  toMap, toSet, toObject, toArray being used instead. The Join operation was also left out.

Written in Typescript,under the hood, LinQer uses generators and with that lazy evaluation. Therefore we now we can limit the operations to just the items of the collection under transformation that are of interest.

LinQer is split in two parts, Linqer.slim.js and Linqer.js. The former exposes the basic methods while the latter all (well most) of the original .NET Enumerable methods including those of Slim, hence you get to choose the one suited to the circumstance at hand. For the full reference of the supported methods head over to its Github repo.

Aside from the repo, the author now offers the library as an npm package making its instalation as simple as:

$ npm install @siderite/linqer

linqersqLastly, a tip for quickly learning to write Linq queries is to get hold of the amazing .NET LINQPad tool and play with it in order to understand how the queries work.Then you can just port them verbatim through LinQer to your JavaScript programs and enjoy the power of Linq in JavaScript!

 

More Information

LInQer 

Related Articles

The LINQ Principle

Linq and XML

Learn Ramda.js The Interactive Way

Functional Programming Patterns With RamdaJS

To be informed about new articles on I Programmer, sign up for our weekly newsletter, subscribe to the RSS feed and follow us on Twitter, Facebook or Linkedin.

Banner


MAUI Improves Android App Start Time
19/10/2021

The latest preview of .NET Multi-platform App UI (MAUI) is now available with .NET 6 RC2 and in the preview channel of Visual Studio 2022 Preview 5. Among the improvements to the new release are perfo [ ... ]



Swift 5.5 Adds Concurrency Support
05/10/2021

Swift 5.5 has been released with new features for asynchronous code and concurrency including async/await, structured concurrency, and Actors.  Swift is Apple's language that was originally devel [ ... ]


More News

{laodposition comment}

Last Updated ( Tuesday, 25 February 2020 )