Use Javascript to implement Linq Query

Source: Internet
Author: User

As an important technology of the. net platform, Linq stands for Language Integrated Query. By constructing a quick query statement, you can quickly filter datasets from a database or collection and operate on memory data in the same way as a database. In Versions later than ECMAScript 5th, Javascript has implemented limited Linq query methods, including forEach, every, some, filter, map, reduce and reduceRight. first, you must note that these methods are not cross-browser and have restrictions on the version. We know that the object of Linq needs to implement the Enumerable interface. This article mainly introduces how to use JS simulation to implement the Linq query in C #, including aggregation query, iterative query, conditional query, and Selector queryer. In essence, Javascript does not support class inheritance. Through property inheritance, class object-oriented functions can be implemented. Therefore, this is also considered an object-oriented method, this means that you can use its attributes to build more object-oriented interfaces. For example, Array is inherited from Array. prototype. If Array. prototype is changed, the Array inherited based on this attribute will inevitably change. With these bases, we began to build our Linq functions. For example, the js api does not support the union method, but supports the concat method to merge data. Array. prototype. union: Let's take a look. NET simple query method var someArray = new int [] {1, 2, 3, 4}; var otherArray = someArray. select (t => t * 2); in C #, when querying data, Select is used and a Delegate is used to construct the query. In this example, t => t * 2 is a Lambda expression. Graft this function to JS and define a Select query under function () {} JS, which can be var someArray = [1, 2, 3, 4]; var otherArray = someArray. select (function (t) {return t * 2}); then define comparison, SortComparer, Predicate, and Selector) compare, sort, condition, and queryer Javascript Linq queryer Select traverses each element under the element and calls JS. the Call method returns data. Copy the code 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 (context, this [I], I, this); return arr ;}; var arr = [1, 2, 3, 4, 5]; var doubled = arr. select (function (t) {return t * 2}); copy the code select?select=take Array. prototype. take = function (c) {return th Is. slice (0, c) ;}; var arr = [1, 2, 3, 4, 5]; var res = arr. take (2); Skip skips the specified number and returns the set 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 First element of the sequence. If no element exists, you can specify a default element. Copy the code Array. prototype. first = function (predicate, def) {var l = this. length; if (! Predicate) return l? This [0]: def = null? Null: def; for (var I = 0; I <l; I ++) if (predicate (this [I], I, this) 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 10. Copy the code Union and merge the data in the two sets. Use concat instead of duplicate data. Copy the code 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); copy the code Distinct to find non-repeated data. If there are duplicate elements, only one element is pushed into the set. Copy the code 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. val = B. val}); // return [{Name: "A", Val: 1}] copy the code IndexOf to find the location where the specified value appears for the first time. Copy the code 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 copy the code Remove to Remove the specified element from the set. Copy the code 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] copy the OrderBy code to copy the code Array. prototype. orderBy = function (selector, comparer) {comparer = comparer | DefaultSortComparer; var arr = this. slice (0); var fn = function (a, B) {return comparer (selector (a), selector (B) ;}; arr. thenBy = function (selector, comparer) {comparer = comparer | DefaultSortComparer; return arr. or DerBy (defaselecselector, function (a, B) {var res = fn (a, B); return res = 0? Comparer (selector (a), selector (B): res ;}; arr. thenByDescending = function (selector, comparer) {comparer = comparer | DefaultSortComparer; return arr. orderBy (defaselecselector, function (a, B) {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 (. toUpperCase ()> B. toUpperCase () return 1; if (. toUpperCase () <B. toUpperCase () return-1; return 0 ;}); copy the code Or DerByDescending: copy the code 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}); copy the code GroupBy to copy the code Array. prototype. groupBy = Function (selector, comparer) {var grp = []; 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;}; Array. prototype. groupBy = function (selector, comparer) {var grp = []; 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;}; copy the code Javascript Linq aggregate Min copy the code 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 copy the code Max copy the code 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. Copy the code Sum and copy the code 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 copy the code Javascript Linq condition to query Where copy the code Array. prototype. where = Array. protot Ype. 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] Copy code Any copy code Array. prototype. any = function (predicate, context) {context = context | Window; var f = this. some | function (p, c) {var l = this. length; if (! P) return l> 0; 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 (); // truevar res2 = arr. any (function (t) {return t> 5}); // false copy code All copy code 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}); // copy the Code Contains to copy the code Array. prototype. contains = function (o, comparer) {comparer = 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", Val: 1}, function (a, B) {return. val = B. val}); // true copy the code javasrt RT Linq iteration ForEach copy the code Array. prototype. forEach = Array. prototype. 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) ;}); copy the DefaultIfEmpty Array code. prototype. defaultIfEmpty = function (val) {return 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]

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.