Nodejs in Linq.js Learning

Source: Internet
Author: User

I. Adding a LINQ module to a Package.json file in Nodejs

such as: "LINQ": "3.0.5"

The Execute NPM Install LINQ module in the terminal is automatically downloaded to Node_modules

Sample/tutorial.js is Linq.js's demo can refer to

Two. Use

Using LINQ and Lambda in JavaScript
    • extends directly to array and string, and can be used directly
    • Also expanded the number, Date, console part of the method, easy to use

Such as

var Enumerable = require (' LINQ ');

var a = Enumerable.from ([' Javascript ', ' Java ', ' C # ', ' php ', ' HTML5 '). (). (). ToArray ();

var c = enumerable.from ([' Javascript ', ' Java ', ' C # ', ' php ', ' HTML5 '). WHERE ("E=>e.startswith (' J ')"). ToArray ();

1. Create an anonymous function from a lambda expression

such as var func = Enumerable.Utils.createLambda (' x=>x*2 ');

Func (5)//10

2.repeat (count) A new array that consists of count elements

var d2 = Enumerable.repeat ([' A ', ' B '],3). ToArray ();

3. Conditional query: Where

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:21},            {name: "Lilei", age:18},            {name: "J Ohn ", age:14},            {Name:" Lintao ", age:25}    ];    var arrres = Enumerable.from (myList). Where ("x=>x.name== ' Jim '"). ToArray ();

The result of Arrres is [{"Name": "Jim", "Age": 20}]

Let's take a look at this prototype of writing Lamada expressions:

var arrres = Enumerable.from (myList). Where (function (i) {return i.name== ' Jim ';});

The parameter i is the entity model of the corresponding collection, and the return type is type bool. There is no definition of Where the extension function in C # looks like: public static ienumerable<tsource> where<tsource> (This Ienumerable<tsource > Source, Func<tsource, bool> predicate), parameter func<tsource, bool> predicate for anonymous delegates, need to pass in the entity model TSource, The return value is then the bool type. In fact, the use of LINQ to JS is to refer to the definition in C #.

4. Condition Selection: Select

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:21},            {name: "Lilei", age:18},            {name: "J Ohn ", age:14},            {Name:" Lintao ", age:25}    ];    var arrres = Enumerable.from (myList). Select ("x=>x.age*10"). ToArray ();

Arrres getting results [200,210,180,140,250]

5. Sorting, de-weight: order, Distinct

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:21},            {name: "Lilei", age:18},            {name: "J Ohn ", age:14},            {Name:" Lintao ", age:25}    ];    var arrres = Enumerable.from (myList). ("X=>x.age"). ToArray ();//Descending orderbydescending ()

The resulting results are sorted by age.

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:20},            {name: "Lilei", age:20},            {name: " John ", age:14},            {Name:" Lintao ", age:25}    ];    var arrres = Enumerable.from (myList). Distinct ("X=>x.age"). ToArray ();

The number of result sets obtained is 3: [{name: "Jim", age:20}, {name: "John", age:14}, {name: "Lintao", age:25}].

6. Traversal: ForEach

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:20},            {name: "Lilei", age:20},            {name: "J Ohn ", age:14},            {Name:" Lintao ", age:25}    ];    Enumerable.from (myList). ForEach (function (value, index) {         document.write ("value =" +value+ ", index =" +index);    });

Obviously two parameters: one is the value, the other is the current index

7. Take a unique object: First, FirstOrDefault, last, LastOrDefault, single, Singleordefault

var myList = [            {name: "Jim", age:20},            {name: "Kate", age:20},            {name: "Lilei", age:20},            {name: "John", age:14},            {Name: "Lintao", age:25}    ];    var arrres = Enumerable.from (myList). FirstOrDefault ("x=>x.age>18");

Several other usages are similar to this one. There's nothing to say about this.

8. Skip, take

Enumerable.range (1,10). Skip (5)//result [6,7,8,9,10]enumerable.range (1,10). Take (5)//result [1,2,3,4,5]

9. Take intersection, set of differences, merge

var array1 = [1,412,5,3,5,412,7];var array2 = [20,12,5,5,7,310]; Enumerable.from (array1). Except (array2)//Result 3,412,1
var array1 = [1,412,5,3,5,412,7];var array2 = [20,12,5,5,7,310]; Enumerable.from (array1). Intersect (array2)//Result 5,7
var array1 = [1,412,5,3,5,412,7];var array2 = [20,12,5,5,7,310]; Enumerable.from (array1). Union (array2)//result is all values in two result set, and auto-weight

Nodejs in Linq.js Learning

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.