text: /* $linq Version 1.2 (by Kurtis Jones @ jscriptlinq.codeplex.com) */(function(e,t){var n={};n.map=function(e,r){var i=arguments[2];if(e.map!==t)return e.map(r,i);var s=e.length;if(!n.isFunction(r))throw new TypeError;var o=new Array(s);for(var u=0;u<s;u++){if(u in e)o[u]=r.call(i,e[u],u,e)}return o};n.filter=function(e,r){var i=arguments[2];if(e.filter!==t)return e.filter(r,i);var s=e.length;if(!n.isFunction(r))throw new TypeError;var o=new Array;for(var u=0;u<s;u++){if(u in e){var a=e[u];if(r.call(i,a,u,e))o.push(a)}}return o};n.every=function(e,r){var i=arguments[2];if(e.every!==t)return e.every(r,i);var s=e.length;if(!n.isFunction(r))throw new TypeError;for(var o=0;o<s;o++){if(o in e&&!r.call(i,e[o],o,e))return false}return true};n.some=function(e,n){var r=arguments[2];if(e.some!==t)return e.some(n,r);var i=e.length;for(var s=0;s<i;s++){if(s in e&&n.call(r,e[s],s,e))return true}return false};n.isFunction=function(e){return typeof e=="function"};n.isArray=function(e){return Object.prototype.toString.call(e)==="[object Array]"};n.extendDeferredSort=function(e,t){var n=function(e){return{keySelector:e.keySelector,comparer:e.comparer,reverse:e.reverse,next:e.next==null?t:n(e.next)}};return n(e)};n.createLambda=function(e){if(n.isFunction(e))return e;if(typeof e==typeof ""&&e!==""){if(e.indexOf("=>")<0)return new Function("$,$$,$$$","return "+e);var t=e.match(/^\s*\(?\s*([^)]*)\s*\)?\s*=>(.*)/);return new Function(t[1],"return "+t[2])}return e};n.processDeferredSort=function(e){if(e.deferredSort==null)return;var t=function(e,n,r){var i;if(r.reverse)i=r.comparer(r.keySelector(n),r.keySelector(e));else i=r.comparer(r.keySelector(e),r.keySelector(n));if(i==0){if(r.next==null)return 0;return t(e,n,r.next)}else return i};e.array.sort(function(n,r){return t(n,r,e.deferredSort)});e.deferredSort=null};n.identity=function(e){return e};var r=function(e){var t=true;if(arguments.length>=1&&arguments[1]!=null&&!arguments[1])t=false;if(t)this.array=e==null?[]:e.slice(0);else this.array=e;this.deferredSort=null};r.from=function(e){if(e==null)return new r([]);if(e instanceof r)return e;if(n.isArray(e))return new r(e);if(jQuery&&e instanceof jQuery)return new r($.makeArray(e));return new r([e],false)};r.range=function(e,t,n){if(e==null||isNaN(e))throw new Error("Invalid 'from' value.");if(t==null||isNaN(t))throw new Error("Invalid 'to' value.");if(n==null||isNaN(n))n=1;var i=[];for(var s=e;s<=t;s+=n){i.push(s)}return new r(i,false)};r.repeat=function(e,t){if(t==null||isNaN(t))t=1;var n=[];for(var i=0;i<t;i++){n.push(e)}return new r(n,false)};r.matches=function(e,t,n){if(t==null)throw new Error("Invalid 'pattern' value.");if(e==null)return new r([]);if(typeof e!=typeof "")throw new Error("Parameter 'text' is not a string.");if(n==null)n="";if(n.indexOf("g")<0)n+="g";var i;if(t instanceof RegExp){if(n.indexOf("i")<0&&t.ignoreCase)n+="i";if(n.indexOf("m")<0&&t.multiline)n+="m";i=t.source}else i=t;var s=new RegExp(i,n);var o=e.match(s);return new r(o==null?[]:o,false)};r.prototype={aggregate:function(e,t,r){t=n.createLambda(t);r=n.createLambda(r);if(t==null||!n.isFunction(t))throw new Error("Invalid operation.");if(r!=null&&!n.isFunction(r))throw new Error("Invalid result selector.");n.processDeferredSort(this);var i=this.array.length;if(i==0&&e==null)throw new Error("Cannot aggregate on empty collection when seed is not given.");var s=e==null?this.array[0]:e;for(var o=0;o<i;o++){if(o in this.array)s=t(s,this.array[o])}return r==null?s:r(s)},all:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);return n.every(this.array,e)},any:function(e){e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);if(e==null)return this.array.length>0;return n.some(this.array,e)},average:function(e){e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);var t=this.array.length;var r=0;var i=0;for(var s=0;s<t;s++){if(s in this.array){var o=e==null?this.array[s]:e(this.array[s]);if(o==null)continue;if(isNaN(o))throw new Error("Encountered an element that is not a number.");r+=o;i+=1}}return r/i},batch:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid result selector.");if(e==null||isNaN(e)||e<=0)throw new Error("Invalid size.");n.processDeferredSort(this);var i=[];var s=0;var o=this.array.length;var u=null;for(var a=0;a<o;a++){if(a in this.array){if(u==null){u=[];i.push(u)}u[s]=t==null?this.array[a]:t(this.array[a]);s+=1;if(s==e){u=null;s=0}}}return new r(i,false)},concat:function(e){n.processDeferredSort(this);if(e==null)return new r(this.array);var t=r.from(e);n.processDeferredSort(t);return new r(this.array.concat(t.array),false)},contains:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);return n.some(this.array,function(n){if(t==null)return n===e;else return t(n,e)})},count:function(e){e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);if(e==null)return this.array.length;var t=this.array.length;var r=0;for(var i=0;i<t;i++){if(i in this.array&&e(this.array[i]))r+=1}return r},defaultIfEmpty:function(e){n.processDeferredSort(this);if(this.array.length==0)return new r([e],false);else return new r(this.array)},distinct:function(e){return this.distinctBy(n.identity,e)},distinctBy:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);return this.groupBy(e,null,t).select(function(e){return(new r(e.values,false)).first()})},elementAt:function(e){if(e==null||isNaN(e)||e<0||e>=this.array.length)throw new Error("Invalid index.");n.processDeferredSort(this);return this.array[e]},elementAtOrDefault:function(e,t){if(e==null||isNaN(e)||e<0||e>=this.array.length)return t;n.processDeferredSort(this);return this.array[e]},equiZip:function(e,t){t=n.createLambda(t);if(t==null||!n.isFunction(t))throw new Error("Invalid result selector.");n.processDeferredSort(this);var i=r.from(e);n.processDeferredSort(e);if(this.array.length!=i.array.length)throw new Error("The two collections being equi-zipped are not of equal lengths.");var s=this.array.length;var o=[];for(var u=0;u<s;u++){o.push(t(this.array[u],i.array[u]))}return new r(o,false)},except:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);if(e==null)return new r(this.array);var i=r.from(e);n.processDeferredSort(i);var s=this.array.length;var o=new r([],false);for(var u=0;u<s;u++){if(u in this.array){var a=this.array[u];var f=function(e){if(t==null)return e===a;else return t(e,a)};var l=n.some(o.array,f);var c=n.some(i.array,f);if(!l&&!c)o.array.push(a)}}return o},first:function(e){e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;for(var r=0;r<t;r++){if(r in this.array&&(e==null||e(this.array[r])))return this.array[r]}throw new Error("No first item was found in collection.")},firstOrDefault:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid predicate.");n.processDeferredSort(this);var r=this.array.length;for(var i=0;i<r;i++){if(i in this.array&&(t==null||t(this.array[i])))return this.array[i]}return e},foreach:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid action.");n.processDeferredSort(this);var t=this.array.length;for(var r=0;r<t;r++){if(r in this.array)e(this.array[r],r)}},groupBy:function(e,t,i){e=n.createLambda(e);t=n.createLambda(t);i=n.createLambda(i);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid element selector.");if(i!=null&&!n.isFunction(i))throw new Error("Invalid key comparer.");n.processDeferredSort(this);var s=this.array.length;var o=new r([],false);for(var u=0;u<s;u++){var a=this.array[u];var f=e(a);var l=t==null?a:t(a);var c=o.firstOrDefault(null,function(e){if(i==null)return e.key===f;else return i(e.key,f)});if(c==null)o.array.push({key:f,values:[l]});else c.values.push(l)}return o},groupJoin:function(e,t,i,s,o){t=n.createLambda(t);i=n.createLambda(i);s=n.createLambda(s);o=n.createLambda(o);if(e==null)throw new Error("Invalid inner collection.");if(t==null||!n.isFunction(t))throw new Error("Invalid outer key selector.");if(i==null||!n.isFunction(i))throw new Error("Invalid inner key selector.");if(s==null||!n.isFunction(s))throw new Error("Invalid result selector.");if(o!=null&&!n.isFunction(o))throw new Error("Invalid key comparer.");n.processDeferredSort(this);var u=r.from(e);var a=u.groupBy(i,null,o);var f=this.array.length;var l=[];for(var c=0;c<f;c++){if(c in this.array){var h=this.array[c];var p=t(h);var d=a.firstOrDefault(null,function(e){if(o==null)return e.key===p;else return o(e.key,p)});l.push(s(h,d==null?[]:d.values))}}return new r(l,false)},index:function(e){if(e==null)e=0;if(isNaN(e))throw new Error("Invalid startIndex");return this.select(function(t,n){return{key:e+n,value:t}})},indexOf:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;for(var r=0;r<t;r++){if(r in this.array&&e(this.array[r]))return r}return-1},indexOfElement:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);var r=this.array.length;for(var i=0;i<r;i++){if(i in this.array){if(t==null&&this.array[i]===e||t!=null&&t(this.array[i],e)){return i}}}return-1},intersect:function(e,t){if(e==null)return new r([],false);t=n.createLambda(t);var i=r.from(e);n.processDeferredSort(i);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);var s=this.array.length;var o=new r([],false);for(var u=0;u<s;u++){if(u in this.array){var a=this.array[u];var f=function(e){if(t==null)return e===a;else return t(e,a)};var l=n.some(o.array,f);var c=n.some(i.array,f);if(!l&&c)o.array.push(a)}}return o},join:function(e,t,i,s,o){t=n.createLambda(t);i=n.createLambda(i);s=n.createLambda(s);o=n.createLambda(o);if(e==null)throw new Error("Invalid inner collection.");if(t==null||!n.isFunction(t))throw new Error("Invalid outer key selector.");if(i==null||!n.isFunction(i))throw new Error("Invalid inner key selector.");if(s==null||!n.isFunction(s))throw new Error("Invalid result selector.");if(o!=null&&!n.isFunction(o))throw new Error("Invalid key comparer.");n.processDeferredSort(this);var u=r.from(e);var a=u.groupBy(i,null,o);var f=this.array.length;var l=[];for(var c=0;c<f;c++){if(c in this.array){var h=this.array[c];var p=t(h);var d=a.firstOrDefault(null,function(e){if(o==null)return e.key===p;else return o(e.key,p)});if(d!=null&&d.values.length>0){var v=d.values.length;for(var m=0;m<v;m++){l.push(s(h,d.values[m]))}}}}return new r(l,false)},last:function(e){e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);for(var t=this.array.length-1;t>=0;t--){if(t in this.array&&(e==null||e(this.array[t])))return this.array[t]}throw new Error("No last item was found in collection.")},lastIndexOf:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;for(var r=t-1;r>=0;r--){if(r in this.array&&e(this.array[r]))return r}return-1},lastIndexOfElement:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);var r=this.array.length;for(var i=r-1;i>=0;i--){if(i in this.array){if(t==null&&this.array[i]===e||t!=null&&t(this.array[i],e)){return i}}}return-1},lastOrDefault:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid predicate.");n.processDeferredSort(this);for(var r=this.array.length-1;r>=0;r--){if(r in this.array&&(t==null||t(this.array[r])))return this.array[r]}return e},min:function(e){if(this.array.length==0)throw new Error("No minimum element.");e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);var t=this.array.length;var r=e==null?this.array[0]:e(this.array[0]);for(var i=1;i<t;i++){if(i in this.array){var s=e==null?this.array[i]:e(this.array[i]);if(s<r)r=s}}return r},minBy:function(e){if(this.array.length==0)throw new Error("No mininum element.");e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);var t=this.array.length;var r=e(this.array[0]);var i=this.array[0];for(var s=1;s<t;s++){if(s in this.array){var o=this.array[s];var u=e(o);if(u<r){r=u;i=o}}}return i},max:function(e){if(this.array.length==0)throw new Error("No maximum element.");e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);var t=this.array.length;var r=e==null?this.array[0]:e(this.array[0]);for(var i=1;i<t;i++){if(i in this.array){var s=e==null?this.array[i]:e(this.array[i]);if(s>r)r=s}}return r},maxBy:function(e){if(this.array.length==0)throw new Error("No maximum element.");e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);var t=this.array.length;var r=e(this.array[0]);var i=this.array[0];for(var s=1;s<t;s++){if(s in this.array){var o=this.array[s];var u=e(o);if(u>r){r=u;i=o}}}return i},orderBy:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");if(t==null)t=function(e,t){return e-t};n.processDeferredSort(this);var i=new r(this.array);i.deferredSort={keySelector:e,comparer:t,reverse:false,next:null};return i},orderByDescending:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");if(t==null)t=function(e,t){return e-t};n.processDeferredSort(this);var i=new r(this.array);i.deferredSort={keySelector:e,comparer:t,reverse:true,next:null};return i},pad:function(e,t){if(e==null||isNaN(e))throw new Error("Invalid width.");n.processDeferredSort(this);var i=this.array.length;if(i>=e)return new r(this.array);return new r(this.array.concat(r.repeat(t,e-i).array),false)},padWith:function(e,t){if(e==null||isNaN(e))throw new Error("Invalid width");t=n.createLambda(t);if(t==null||!n.isFunction(t))throw new Error("Invalid padding selector.");n.processDeferredSort(this);var i=this.array.length;if(i>=e)return new r(this.array);var s=[];for(var o=i;o<e;o++){s.push(t(o))}return new r(this.array.concat(s),false)},pipe:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid action.");n.processDeferredSort(this);var t=this.array.length;for(var i=0;i<t;i++){if(i in this.array){e(this.array[i],i)}}return new r(this.array)},prepend:function(e){n.processDeferredSort(this);return new r([e].concat(this.array),false)},prescan:function(e,t){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid operation.");n.processDeferredSort(this);var i=this.array.length;if(i==0)return new r([],false);var s=t;var o=[s];for(var u=0;u<i-1;u++){if(u in this.array){s=e(s,this.array[u]);o.push(s)}}return new r(o,false)},reverse:function(){n.processDeferredSort(this);return new r(this.array.reverse(),false)},scan:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid operation.");n.processDeferredSort(this);var t=this.array.length;if(t==0)throw new Error("Cannot scan on empty collection.");var i=this.array[0];var s=[i];for(var o=1;o<t;o++){if(o in this.array){i=e(i,this.array[o]);s.push(i)}}return new r(s,false)},select:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid selector.");n.processDeferredSort(this);return new r(n.map(this.array,e),false)},selectMany:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid collection selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid result selector.");n.processDeferredSort(this);var i=this.array.length;var s=[];for(var o=0;o<i;o++){var u=this.array[o];var a=e(u,o);if(!n.isArray(a))a=r.from(a).toArray();if(a!=null&&a.length>0){var f=a.length;for(var l=0;l<f;l++){var c=a[l];var h=t==null?c:t(c,u);s.push(h)}}}return new r(s,false)},sequenceEqual:function(e,t){if(e==null)return false;t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer");n.processDeferredSort(this);var i=r.from(e);n.processDeferredSort(i);var s=this.array.length;if(s!=i.array.length)return false;for(var o=0;o<s;o++){if(t==null){if(this.array[o]!==i.array[o])return false}else if(!t(this.array[o],i.array[o]))return false}return true},sequenceEquivalent:function(e,t){if(e==null)return false;t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);var i=r.from(e);n.processDeferredSort(i);var s=this.array.length;var o=i.array.length;if(s!=o)return false;var u=this.toLookup("x => x",t);var a=i.toLookup("x => x",t);return u.count()==a.count()&&u.all(function(e){var n=a.firstOrDefault(null,function(n){if(t==null)return n.key===e.key;else return t(n.key,e.key)});if(n==null)return false;return e.values.length==n.values.length})},single:function(e){e=n.createLambda(e);if(e==null){if(this.array.length==0)throw new Error("No single item in the collection.");if(this.array.length>1)throw new Error("More than one item in the collection.");n.processDeferredSort(this);return this.array[0]}if(!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;var r=false;var i=null;for(var s=0;s<t;s++){if(s in this.array&&e(this.array[s])){if(r)throw new Error("More than one item matched the predicate in the collection.");r=true;i=this.array[s]}}if(!r)throw new Error("No single item matched the predicate in the collection.");return i},singleOrDefault:function(e,t){t=n.createLambda(t);if(t==null){if(this.array.length==0)return e;if(this.array.length>1)throw new Error("More than one item in the collection.");n.processDeferredSort(this);return this.array[0]}if(!n.isFunction(t))throw new Error("Invalid predicate.");n.processDeferredSort(this);var r=this.array.length;var i=false;var s=null;for(var o=0;o<r;o++){if(o in this.array&&t(this.array[o])){if(i)throw new Error("More than one item matched the predicate in the collection.");i=true;s=this.array[o]}}return i?s:e},singleOrFallback:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid fallback");n.processDeferredSort(this);if(this.array.length==0)return e();else if(this.array.length==1)return this.array[0];else throw new Error("More than one item in the collection.")},skip:function(e){if(e==null||isNaN(e))throw new Error("Invalid count.");n.processDeferredSort(this);return new r(this.array.slice(e),false)},skipUntil:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate");return this.skipWhile(function(t){return!e(t)})},skipWhile:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;var i=[];var s=true;for(var o=0;o<t;o++){var u=this.array[o];if(!s)i.push(u);else if(!e(u)){s=false;i.push(u)}}return new r(i,false)},sum:function(e){n.processDeferredSort(this);if(this.array.length==0)return 0;e=n.createLambda(e);if(e!=null&&!n.isFunction(e))throw new Error("Invalid selector.");var t=this.array.length;var r=0;for(var i=0;i<t;i++){if(i in this.array){var s=e==null?this.array[i]:e(this.array[i]);if(s!=null)r+=s}}return r},take:function(e){if(e==null||isNaN(e))throw new Error("Invalid count.");n.processDeferredSort(this);return new r(this.array.slice(0,e),false)},takeEvery:function(e){if(e==null||isNaN(e))throw new Error("Invalid count.");n.processDeferredSort(this);return this.where(function(t,n){return n%e==0})},takeLast:function(e){if(e==null||isNaN(e))throw new Error("Invalid count.");n.processDeferredSort(this);if(e<=0)return new r([],false);if(e>this.array.length)e=this.array.length;return new r(this.array.slice(this.array.length-e),false)},takeUntil:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate");return this.takeWhile(function(t){return!e(t)})},takeWhile:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);var t=this.array.length;var i=[];var s=true;for(var o=0;o<t&&s;o++){var u=this.array[o];if(!e(u))s=false;else i.push(u)}return new r(i,false)},thenBy:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");if(this.deferredSort==null)throw new Error("ThenBy can only be called following an OrderBy/OrderByDescending.");if(t==null)t=function(e,t){return e-t};var i=new r(this.array);i.deferredSort=n.extendDeferredSort(this.deferredSort,{keySelector:e,comparer:t,reverse:false,next:null});return i},thenByDescending:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");if(this.deferredSort==null)throw new Error("ThenByDescending can only be called following an OrderBy/OrderByDescending.");if(t==null)t=function(e,t){return e-t};var i=new r(this.array);i.deferredSort=n.extendDeferredSort(this.deferredSort,{keySelector:e,comparer:t,reverse:true,next:null});return i},toDelimitedString:function(e){if(e==null)e=",";n.processDeferredSort(this);return this.array.join(e)},toDictionary:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid element selector.");n.processDeferredSort(this);var r=this.array.length;var i={};for(var s=0;s<r;s++){var o=this.array[s];var u=e(o);if(u in i)throw new Error("Duplicate key in collection.");i[u]=t==null?o:t(o)}return i},toJQuery:function(){if(!jQuery)throw new Error("Cannot return jQuery object--jQuery not available.");n.processDeferredSort(this);return jQuery(this.array)},toLookup:function(e,t){e=n.createLambda(e);t=n.createLambda(t);if(e==null||!n.isFunction(e))throw new Error("Invalid key selector.");if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer");n.processDeferredSort(this);var i=new r([],false);var s=this.array.length;for(var o=0;o<s;o++){if(o in this.array){var u=this.array[o];var a=e(u);var f=i.firstOrDefault(null,function(e){if(t==null)return e.key===a;else return t(e.key,a)});if(f==null){f={key:a,values:[]};i.array.push(f)}f.values.push(u)}}return i},union:function(e,t){t=n.createLambda(t);if(t!=null&&!n.isFunction(t))throw new Error("Invalid comparer.");n.processDeferredSort(this);var i=r.from(e==null?[]:e);n.processDeferredSort(i);var s=this.array.length;var o=new r([],false);for(var u=0;u<s;u++){if(u in this.array){var a=this.array[u];var f=n.some(o.array,function(e){if(t==null)return e===a;else return t(e,a)});if(!f)o.array.push(a)}}s=i.array.length;for(var u=0;u<s;u++){if(u in i.array){var a=i.array[u];var f=n.some(o.array,function(e){if(t==null)return e===a;else return t(e,a)});if(!f)o.array.push(a)}}return o},where:function(e){e=n.createLambda(e);if(e==null||!n.isFunction(e))throw new Error("Invalid predicate.");n.processDeferredSort(this);return new r(n.filter(this.array,e),false)},zip:function(e,t){t=n.createLambda(t);if(t==null||!n.isFunction(t))throw new Error("Invalid result selector.");n.processDeferredSort(this);if(e==null)return new r([],false);var i=r.from(e);n.processDeferredSort(i);var s=Math.min(this.array.length,i.array.length);var o=[];for(var u=0;u<s;u++){o.push(t(this.array[u],i.array[u]))}return new r(o,false)},zipLongest:function(e,t,i,s){s=n.createLambda(s);if(s==null||!n.isFunction(s))throw new Error("Invalid result selector.");n.processDeferredSort(this);var o=r.from(e);n.processDeferredSort(o);var u=Math.max(this.array.length,o.array.length);var a=[];for(var f=0;f<u;f++){a.push(s(f>=this.array.length?t:this.array[f],f>=o.array.length?i:o.array[f]))}return new r(a,false)},toArray:function(){n.processDeferredSort(this);return this.array.slice(0)}};e.linq=r;e.$linq=r.from})(window)