LINQ is an important technology for the. NET platform, full name language integrated Query. By building a quick query statement, you can quickly filter data sets from a database or collection and manipulate memory data in the same way as querying a database.
In the ECMAScript 5th version, JavaScript implements a limited, limited LINQ query, including foreach, every, some, filter, map, reduce and re Duceright. First of all, it needs to be explained that these methods are not cross-browser and have a corresponding limitation on the version. We know that LINQ objects need to implement the enumerable interface, this article mainly introduces the use of JS simulation to achieve C # LINQ query, including clustered query, iterative query, conditional query, build selector query and so on. JavaScript does not support class inheritance in nature, and can implement object-oriented functionality through inheritance of attributes, so this is also considered an object-oriented approach, which means that you can use its properties to build more object-oriented interfaces. For example, array, which is inherited from Array.prototype. If you change the Array.prototype, the array that is inherited based on this attribute is bound to change. With these references, we start building our LINQ capabilities. For example, the JS API does not support the Union method, but it supports concat methods for merging data. Array.prototype.union First take a look at one. NET simple query mode var somearray = new int[] {1, 2, 3, 4}; var otherarray = somearray.select (t => t * 2); using the query data under C # is a SELECT, using a Delegate to build the query. In this example, we used the t => T * 2 to be a lambda expression. This function is grafted to JS, the definition of a function () {} JS select query can be so var somearray = [1, 2, 3, 4]; var Otherarray = Somearray.select (function (t) {return T * 2}); then define a comparison (equalitycomparer), sort (sortcomparer), condition (Pred icate), finder (SElector) Compare, sort, condition, query Javascript Linq query select Iterate through each element of the element and invoke the Js.call method to return the data. Array.prototype.select = Array.prototype.map | | Function (Selector, context) { context = context | | window; var arr = []; var l = This.length; for (var i = 0; i < L; i++) Arr.push (Selector.call, this[i), I, this) ); return arr; }; var arr = [1, 2, 3, 4, 5]; var doubled = Arr.select (function (t) {return T * 2}); SelectMany SelectMany &NBS P Take Array.prototype.take = function (c) { return This.slice (0, c);}; var arr = [1, 2, 3, 4, 5]; var res = arr.take (2); Skip Skip the specified number and return the collection data using slice. Array.prototype.skip = function (c) { return This.slice (c);}; var arr = [1, 2, 3, 4, 5]; var res = Arr.skip (2); First Returns the number one element of the sequence, and if there are no elements, you can specify a default element. Array.prototype.first = function (predicate, def) { var l = this.length; if (!PR Edicate) return l? This[0]: def = null? Null:def; for (var i = 0; i < L; i++) if (predicate (this[i), I, this) &N Bsp return this[i]; Return def = null? Null:def; }; var arr = [1, 2, 3, 4, 5]; var T1 = Arr.first (); var t2 = Arr.first (function (t) {return T > 2}); var t3 = Arr.first (function (t) {return T > 10}, 10); The default value is Union merging data from two collections, using concat to not merge duplicate data. Array.prototype.union = function (arr) { return This.concat (arr). distinct (); var arr1 = [1, 2, 3, 4, 5]; var arr2 = [5, 6, 7, 8, 9]; var res = arr1.union (ARR2); DISTINCT Find data that is not duplicated. When a repeating element is pushed only one element into the collection. ARRAY.PROTOTYPE.DISTINCT = function (Comparer) { var arr = []; var L = this.length for (var i = 0; i < L; i++) { if (!arr.contains (This[i], comparer)) Arr.push (this[ I]); } return arr; }; var arr1 = [1, 2, 2, 3, 3, 4, 5, 5]; var res1 = arr.distinct (); //[1, 2, 3, 4, 5] var arr2 = [{name: ' A ', val:1}, {name: ' B ', val:1}]; var res2 = arr2.distinct (function (A, b) {return a.val = = B.val}); Returns [{Name: ' A ', val:1}] IndexOf finds the location of the specified value for the first occurrence. Array.prototype.indexOf = Array.prototype.indexOf | | function (o, index) { var L = this.length for (var i = Math.max (math.min (Index, L), 0) | | 0; I < L; i++) if (this[i] = = O) return i; return-1; }; var arr = [1, 2, 3, 4, 5]; var index = Arr.indexof (2); //1 Remove   Removes the specified element from the collection. Array.prototype.remove = function (item) { var i = This.indexof (item); if (i!= -1 This.splice (i, 1); }; var arr = [1, 2, 3, 4, 5]; Arr.remove (2); //[1, 3, 4, 5] by Array.prototype.orderBy = function (selector, comparer) { &NBSP ; Comparer = Comparer | | Defaultsortcomparer; var arr = this.slice (0); var fn = function (A, b) { return comparer (selector (a), selector (b)); &NB Sp }; Arr.thenby = function (selector, comparer) { comparer = Comparer | | Defaultsortcomparer; return Arr.orderby (Defaultselector, function (A, b) { &NBSP ; var res = fn (A, b); return res = = 0? Comparer (selector (a), selector (b)): res; }); }; arr.thenbydescending = function (selector, comparer) { comparer = Compare R | | Defaultsortcomparer; return Arr.orderby (Defaultselector, function (A, b) { &NBSP ; var res = fn (A, b); return res = = 0? -comparer (Selector (a), selector (b)): res; }); }; return Arr.sort (FN); }; var arr = [{name: "a", val:1}, {name: "a", val:2}, {name: "B", val:1}, {name: "C", Val:2}]; var res1 = Arr.orderby (function (t) {return t.name}); var res2 = Arr.orderby (function (t) {return t.name}, function (A, b) { if (A.touppercase () > B.touppercase ()) return 1; if (A.touppercase () < B.touppercase ()) return-1; return 0; }); OrderByDescending ARRAY.Prototype.orderbydescending = function (selector, comparer) { comparer = Comparer | | Defaultsortcomparer; return This.orderby (selector, function (A, b) {return-comparer (A, B)}); }; var arr = [{name: "a", val:1}, {name: "a", val:2}, {name: "B", val:1}, {name: "C", val:2}]; var res = arr.orderbydescending (function (t) {return t.name}); GroupBy Array.prototype.groupBy = function (selector, comparer) { var grp = []; nbsp var L = this.length; comparer = Comparer | | Defaultequalitycomparer; Selector = Selector | | Defaultselector; for (var i = 0; i < L; i++) { var k = selector (this[i]); var g = Grp.first (function (u) {return comparer (U.key, k);}); if (!g) { g = []; G.key = k; &NBSp Grp.push (g); } G.push (this[i]); } return GRP; }; Array.prototype.groupBy = function (selector, comparer) { var grp = []; & nbsp var L = this.length; comparer = Comparer | | Defaultequalitycomparer; Selector = Selector | | Defaultselector; for (var i = 0; i < L; i++) { var k = selector (this[i]); var g = Grp.first (function (u) {return comparer (U.key, k);}); if (!g) { g = []; G.key = k; Grp.push (g); } G.push (this[i]); } return GRP; }; JavascriptLinq aggregation Min Array.prototype.min = function (s) { s = s | | Defaultselector; var l = this.length; var min = s (this[0]); while (l--> 0) if (s (this[l]) < min) min = s (this[l]); return min; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var min1 = Arr.min (); //1 var arr2 = [{name: "A", val:1}, {name: "B", val:2}]; var min2 = arr2.min (function (t) {return t.val}); //1 Max Array.prototype.max = function (s) { s = s | | Defaultselector; var l = this.length; var max = s (this[0]); while (l--> 0) if (s (this[l) > max) max = S (this[l]); return Max; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var max1 = Arr.max (); //8 var arr2 = [{name: "A", val:1}, {name: "B", val:2}]; var max2 = Arr2.max (funCtion (t) {return t.val}); //2 Sum Array.prototype.sum = function (s) { s = s | | Defaultselector; var l = this.length; var sum = 0; while (l--> 0) sum = = S (this[l]); return sum; }; var arr1 = [1, 2, 3, 4, 5, 6, 7, 8]; var sum1 = Arr.sum (); //36 var arr2 = [{name: "A", val:1}, {name: "B", val:2}]; var sum2 = arr2.sum (function (t) {return t.val}); //3 Javascript Linq conditional query Where Array.prototype.where = Array.prototype.filter | | function (predicate, context) { context = context | | window; var arr = []; var l = This.length; for (var i = 0; i < L; i++) if (Predicate.call (context, this[i], I, this) = = = true) Arr.push (This[i]); return arr; }; var arr = [1, 2, 3, 4, 5]; var res = Arr.where (function (t) {return T >2}); //[3, 4, 5] any Array.prototype.any = function (predicate, context) { cont Ext = Context | | Window var f = This.some | | function (P, c) { var l = this.length; if (!p) return l > 0; &n Bsp while (l--> 0) if (P.call (c, This[l], l, this) = = True ) return true; return false; }; Return f.apply (this, [predicate, context]); }; var arr = [1, 2, 3, 4, 5]; var res1 = Arr.any (); //true var res2 = Arr.any (function (t) {return T > 5}); //false All Array.prototype.all = function (predicate, context) { context = Context | | Window predicate = predicate | | Defaultpredicate; var f = This.every | | function (P, c) { return thIs.length = = This.where (p, c). length; }; Return f.apply (this, [predicate, context]); }; var arr = [1, 2, 3, 4, 5]; var res = Arr.all (function (t) {return T < 6}); //true Contains Array.prototype.contains = function (o, comparer) { Compar ER = Comparer | | Defaultequalitycomparer; var l = this.length; while (l--> 0) if (comparer (this[l), O) = true) return true; return false; }; var arr1 = [1, 2, 3, 4, 5]; var res1 = arr.contains (2); //true var arr2 = [{name: "A", val:1}, {name: "B", val:1}]; var res2 = arr2.contains ({name: "C", Va L:1}, function (A, b) {return a.val = = = B.val}); //true JAVASCIPRT Linq Iteration ForEach Array.prototype.forEach = Array.prototyp E.foreach | | function (callback, context) { context = Context | | window; var l = this.length; for (var i = 0; i < L; i++) Callback.call (context, this[i], I, this); }; var arr = [1, 2, 3, 4, 5]; Arr.foreach (function (t) {if (t% 2 ==0) Console.log (t);}); DefaultIfEmpty Array.prototype.defaultIfEmpty = function (val) { RE Turn this.length = = 0? [val = = null? Null:val]: this; }; var arr = [1, 2, 3, 4, 5]; var res = Arr.where (function (t) {return T > 5}). DefaultIfEmpty (5); //[5]