LINQ to JavaScript

17 篇文章 0 订阅

What is LINQ to JavaScript?

LINQ to JavaScript (JSLINQ for short) is an implementation of LINQ to Objects implemented in JavaScript. It is built using a set of extension methods built on top of the JavaScript Array object. If you are using an Array, you can use JSLINQ.

If you don't know what LINQ is; it's a new featureset in the .NET Framework 3.5 that allows more SQL-like querying of any kind of data. In the case of LINQ to JavaScript, it provides the ability to query against Arrays.
var myList = [
            {FirstName:"Chris",LastName:"Pearson"},
            {FirstName:"Kate",LastName:"Johnson"},
            {FirstName:"Josh",LastName:"Sutherland"},
            {FirstName:"John",LastName:"Ronald"},
            {FirstName:"Steve",LastName:"Pinkerton"}
            ];
            
var exampleArray = JSLINQ(myList)
                   .Where(function(item){ return item.FirstName == "Chris"; })
                   .OrderBy(function(item) { return item.FirstName; })
                   .Select(function(item){ return item.FirstName; });

Using LINQ to JavaScript

We will use this Array for the following examples:
var myList = [
            {FirstName:"Chris",LastName:"Pearson"},
            {FirstName:"Kate",LastName:"Johnson"},
            {FirstName:"Josh",LastName:"Sutherland"},
            {FirstName:"John",LastName:"Ronald"},
            {FirstName:"Steve",LastName:"Pinkerton"}
            ];

Create an Instance of the JSLINQ object with your data

You need to create a new JSLINQ object and pass it the javascript array of data that you will be querying.
var example = JSLINQ(myList);

Using the Where operator to specify query criteria

In this case, we're getting all items in the Array that have FirstName property set to Chris.
var whereExample1 = JSLINQ(myList).
                      Where(function(item){ return item.FirstName == "Chris"; });

Using the Select operator to specify which data to return

In this case, we're going to return only the FirstName property of each item in the Array.
var selectTest2 = JSLINQ(myList).
                Select(function(item){ return item.FirstName; });

Using the OrderBy operator to determine how to sort the order of the items in the Array

In this case, we're going to order them by the FirstName property.
var sortTest1 = JSLINQ(myList)
             .OrderBy(function(item){ return item.FirstName; });

Acticles / News

JSLinq Editor
2010/03/16:  LINQ for JavaScript: Using and Extending JSLINQ
2008/01/24:  LINQ to JavaScript (JSLINQ) Open Source Project Launched!

Contributors

This project is maintained by  Chris Pietschmann. He is a Microsoft MVP for Bing Maps, a Co-Founder of  Carto LLC, and the Owner of  Simplovation LLC a software development consulting company that specializes in Mapping/GIS related application development.

Last edited May 4 at 9:58 PM by crpietschmann, version 41


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值