LINQ TO SQL概述
10年积累的网站制作、成都网站制作经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有张家川回族自治免费网站建设让你可以放心的选择与我们合作。
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 ...... 等等 就不一一介绍了
新闻标题:详解LINQtoJavaScript的几种方法
转载源于:http://www.shufengxianlan.com/qtweb/news7/38907.html
网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联