标签:
一.在nodeJs 中package.json文件中添加linq模块
如: "linq" : "3.0.5"
终端中执行npm install linq模块会自动下载到node_modules中
sample/tutorial.js 是linq.js的demo 可以参考
二.使用
如
var Enumerable = require(‘linq‘);
var a = Enumerable.from([‘Javascript‘, ‘Java‘, ‘C#‘, ‘php‘, ‘HTML5‘]).orderBy().toArray();
var c = Enumerable.from([‘Javascript‘, ‘Java‘, ‘C#‘, ‘php‘, ‘HTML5‘]).where("e=>e.startsWith(‘J‘)").toArray();
1.根据lambda表达式创建匿名函数
如 var func = Enumerable.Utils.createLambda(‘x=>x*2‘);
func(5) //10
2.repeat(count) 由count个该元素组成的新数组
var d2 = Enumerable.repeat([‘a‘,‘b‘],3).toArray();
3.条件查询:Where
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 21 }, { Name: "Lilei", Age: 18 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).Where("x=>x.Name==‘Jim‘").ToArray();
arrRes的结果为 [{"Name":"Jim","Age":20}]
我们来看看这种写Lamada表达式的原型:
var arrRes = Enumerable.From(myList).Where(function (i) { return i.Name==‘Jim‘; });
参数i是对应的集合里面的实体模型,返回类型为bool类型。有没有和C#里面的扩展函数Where的定义很像:public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);参数Func<TSource, bool> predicate为匿名委托,需要传入实体模型TSource,然后返回值为bool类型。其实感觉linq to js的使用就是参照c#里面的定义来的。
4.条件选择:Select
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 21 }, { Name: "Lilei", Age: 18 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).Select("x=>x.Age*10").ToArray();
arrRes得到结果 [200,210,180,140,250]
5.排序、去重:OrderBy、Distinct
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 21 }, { Name: "Lilei", Age: 18 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; var arrRes = Enumerable.From(myList).OrderBy("x=>x.Age").ToArray();//降序OrderByDescending()
得到的结果会按照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();
得到的结果集合的数量为3个:[ { Name: "Jim", Age: 20 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 }]。
6.遍历:ForEach
var myList = [ { Name: "Jim", Age: 20 }, { Name: "Kate", Age: 20 }, { Name: "Lilei", Age: 20 }, { Name: "John", Age: 14 }, { Name: "LinTao", Age: 25 } ]; Enumerable.From(myList).ForEach(function(value, index){ document.write("值="+value+",索引="+index); });
很显然两个参数:一个是值,另一个是当前索引
7.取唯一对象: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");
其他几个用法和这个类似。这个没什么好说的。
8 .Skip、Take
Enumerable.Range(1,10).Skip(5)//结果[6,7,8,9,10] Enumerable.Range(1,10).Take(5)//结果[1,2,3,4,5]
9.取交集、取差集、合并
var array1 = [1,412,5,3,5,412,7]; var array2 = [20,12,5,5,7,310]; Enumerable.From(array1).Except(array2) //结果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) //结果5,7
var array1 = [1,412,5,3,5,412,7]; var array2 = [20,12,5,5,7,310]; Enumerable.From(array1).Union(array2) //结果是两个结果集里面所有值,并自动去重
标签:
原文地址:http://www.cnblogs.com/laien/p/5610884.html