详解LINQ to JavaScript的几种方法
LINQ TO SQL概述
LINQ TO SQL是LINQ技术在数据库方面的应用。数据库技术从OLEDB,ODBC到ADO,在到ADO.NET到现在的LINQ TO SQL,让程序员操作数据库越来越简单。
LINQ TO JAVASCRIPT概述
LINQ TO JAVASCRIPT 宗旨就是代码风格像 LINQ TO SQL 一样操作数据。
LINQ TO JAVASCRIPT 数据格式为 JSON (JavaScript Object Notatio)
那我们看看 linq to javascript 是怎么做的
首先,我们先准备静态数据做为演示用。
var Samples = {}; Samples.People = [ { ID: 1, FirstName: "Chris", LastName: "Pearson", BookIDs: [8008, 1002, 1003] }, { ID: 2, FirstName: "Chris", LastName: "Johnson", BookIDs: [2001, 2002, 2003] }, { ID: 3, FirstName: "Josh", LastName: "Sutherland", BookIDs: [3001, 3002, 3003] }, { ID: 4, FirstName: "John", LastName: "Ronald", BookIDs: [4001, 4002, 4003] }, { ID: 5, FirstName: "Steve", LastName: "Pinkerton", BookIDs: [1001, 1002, 1003] }, { ID: 6, FirstName: "Katie", LastName: "Zimmerman", BookIDs: [2001, 2002, 2003] }, { ID: 7, FirstName: "Dirk", LastName: "Anderson", BookIDs: [3001, 3002, 3003] }, { ID: 8, FirstName: "Chris", LastName: "Stevenson", BookIDs: [4001, 4002, 4003] }, { ID: 9, FirstName: "Bernard", LastName: "Sutherland", BookIDs: [1001, 2002, 3003] }, { ID: 10, FirstName: "Kate", LastName: "Pinkerton", BookIDs: [4001, 3002, 2003] } ];
核心代码我们加到了YUI 里
YUI.add('LINQ', function(Y) { // core code },'3.1.1');
用Where关键字查询
var db = Y.LINQ(Samples.People) .Where(function(item){return item.FirstName == 'Chris';})
会返回三条数据
{items => [{ID => 1, FirstName => Chris, LastName => Pearson, BookIDs => [8008, 1002, 1003]}, {ID => 2, FirstName => Chris, LastName => Johnson, BookIDs => [2001, 2002, 2003]}, {ID => 8, FirstName => Chris, LastName => Stevenson, BookIDs => [4001, 4002, 4003]}]} Select var db = Y.LINQ(Samples.People) .Where(function(item){return item.FirstName == 'Chris';}) .Select(function(item){return item.FirstName;})
返回结果
语句中指定了返回 FirstName 所以返回 {items => [Chris, Chris, Chris]}
Intersect
第一个结果集
var test2 = Y.LINQ(Samples.People) .Where(function(item){return item.FirstName != 'Chris';});
返回是
{items => [{ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}, {ID => 5, FirstName => Steve, LastName => Pinkerton, BookIDs => [1001, 1002, 1003]}, {ID => 6, FirstName => Katie, LastName => Zimmerman, BookIDs => [2001, 2002, 2003]}, {ID => 7, FirstName => Dirk, LastName => Anderson, BookIDs => [3001, 3002, 3003]}, {ID => 9, FirstName => Bernard, LastName => Sutherland, BookIDs => [1001, 2002, 3003]}, {ID => 10, FirstName => Kate, LastName => Pinkerton, BookIDs => [4001, 3002, 2003]}]}
第2个结果集
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .Intersect(test2)
先取出所有ID小于5的数据,然后再返回和test2数据集相交的部分
返回结果
{items => [{ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}]}
只返回3,4两条数据了 ,因为名字不等于Chris 并且 ID < 5的 就只有2条数据 : )
OrderBy / OrderByDescending var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .OrderByDescending(function(item){return item.ID})
按指定表达式对集合倒序排序,这里我们用ID排序
返回结果:
{items => [{ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}, {ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 2, FirstName => Chris, LastName => Johnson, BookIDs => [2001, 2002, 2003]}, {ID => 1, FirstName => Chris, LastName => Pearson, BookIDs => [8008, 1002, 1003]}]}
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .OrderBy(function(item){return item.ID})
按指定表达式对集合正序排序,这里我们用ID排序
返回结果:
{items => [{ID => 1, FirstName => Chris, LastName => Pearson, BookIDs => [8008, 1002, 1003]}, {ID => 2, FirstName => Chris, LastName => Johnson, BookIDs => [2001, 2002, 2003]}, {ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}]}
Skip
跳过几条数据
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .OrderBy(function(item){return item.ID}) .Skip(0);
返回结果:
{items => [{ID => 2, FirstName => Chris, LastName => Johnson, BookIDs => [2001, 2002, 2003]}, {ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}]}
从结果看,我们的数据集跳过了第一条数据
Delete
删除
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .OrderBy(function(item){return item.ID}) .Skip(0) .Delete(function(item){return item.ID == 2;})
返回结果:
{items => [{ID => 3, FirstName => Josh, LastName => Sutherland, BookIDs => [3001, 3002, 3003]}, {ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}]}
Min / Max / Average / Sum
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .OrderBy(function(item){return item.ID}) .Skip(0) .Delete(function(item){return item.ID == 2;}) .Min(function(item){return item.ID;})
沿用上面结果集,Min取最小值
最后返回结果 3 ,其他关键字用法相同 : )
First / Last
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .First();
返回首行数据
{ID => 1, FirstName => Chris, LastName => Pearson, BookIDs => [8008, 1002, 1003]}
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .Last();
返回末行数据
{ID => 4, FirstName => John, LastName => Ronald, BookIDs => [4001, 4002, 4003]}
Take
取前几条数据
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .Take(2);
取了前2条数据,返回结果
{items => [{ID => 1, FirstName => Chris, LastName => Pearson, BookIDs => [8008, 1002, 1003]}, {ID => 2, FirstName => Chris, LastName => Johnson, BookIDs => [2001, 2002, 2003]}]}
Count
返回结果的总个数
var test = Y.LINQ(Samples.People) .Where(function(item){return item.ID < 5;}) .Count();
返回结果 4 ,正确的 ID小于5的 数据刚好是4条 : )
SelectMany / Any / All / ElementAt / Distinct ...... 等等 就不一一介绍了