linq to js使用汇总

用途:方便js操作查询json数据。

下载网址:http://jslinq.codeplex.com/

使用方法:只需要引用linq.js即可。

查询方法:

一、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();

 

二、排序:OrderBy

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()

 

 

三、去重:Distinct

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();

 

四、遍历: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);   
});

 

五、取唯一对象: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");

 

六、Skip、Take

Enumerable.Range(1,10).Skip(5)//结果[6,7,8,9,10]
Enumerable.Range(1,10).Take(5)//结果[1,2,3,4,5]

  

 

你可能感兴趣的:(linq to js使用汇总)