LINQ to JavaScript

[索引页]





LINQ to JavaScript是使用JavaScript来做出类似LINQ样的功能,可以做到在客户端对一些数据筛选,加强了对数组的操作.其中有一些功能本来JavaScript就有如对数组数据过滤的功能,再想想就这样筛选数据没有什么大的意义,但要把筛选后的数据又通过JSON反回到服务端那就有用拉.这样可以和Silverlight1.0,asp.net ajax在一起使用,它能对他们起到一定的辅助作用吧.(还有这个只能对数组中使用)


资料:

http://www.nikhilk.net/ScriptAndLINQ.aspx
http://pietschsoft.com/post/2008/01/LINQ-to-JavaScript-(JSLINQ)-Open-Source-Project-Launched!.aspx
http://www.codeplex.com/JSLINQ

一:介绍


LINQ to JavaScript(JSLINQ)是一个LINQ to Object在 javaScript中的实现,它对JavaScript Arrary Objcet构建一些列的扩展方法.如果你使用一个Array,你能使用LINQ to JavaScript.

现在的发布版本是是1.03支持

Operators
- From
- Where
- OrderBy
- Select

Added the OrderByDescending Operator
- Added the SelectMany Operator
- Added the Count Operator
- Added the Distinct Operator
- Added the Any Operator
- Added the All Operator
- Made it so you can access the elements "index" within the clause/predicate of each of the following Operators: Where, Count, Any, All
- Added the Reverse Operator, this also allows access to the elements "index" within the clause/predicate
- Added First Operator, this also allows access to the elements "index" within the clause/predicate
- Added Last Operator, this also allows access to the elements "index" within the clause/predicate
- Added ElementAt Operator
- Added Concat Operator - this is identical to JavaScripts Array.concat method

Intersect Operator
- Added DefaultIfEmpty Operator
- Added ElementAtOrDefault Operator
- Added FirstOrDefault Operator
- Added LastOrDefault Operator


- Unit Test Created
--- Intial testing of the Samples Unit Tests yielded the following speed results for all tests:
------ IE7: ~10 milliseconds
------ FireFox 2: ~10 milliseconds
------ Safari 3 for Windows: ~4 milliseconds


使用
(1)在你的工程中添加JSLINQ.js文件;
(2)在你的页面中添上代码<script type="text/javascript" src="JS/JSLINQ.js"></script>



二:体验


(1)首先写一个数组:

var myList  =  [
            
{FirstName:"Chris",LastName:"Pearson"} ,
            
{FirstName:"Kate",LastName:"Johnson"} ,
            
{FirstName:"Josh",LastName:"Sutherland"} ,
            
{FirstName:"John",LastName:"Ronald"} ,
            
{FirstName:"Steve",LastName:"Pinkerton"}
            ];

(2)添加查询。

var exampleArray  =  From(myList).
                   Where(
" item.FirstName == 'Chris' " ).
                   OrderBy(
" item.FirstName " ).
                   Select(
" item.FirstName " );



结果是:
Chris

其实上面有的那些操作符的使用,你在下载官方的文件时里面就有很多事例,就能让能掌握拉.


我就把筛选数据序列化为JSON穿回服务端.(我一直想做纯JS的网站!)


接着你再添加两个方法:

jsonObjcet = function ()
{
    
this.value = '0';
    
this.text = '';
}

Show 
=   function ()
{
    
var json = new jsonObjcet();
    json.value
='0';
    json.text 
=exampleArray[0];
    
var jsonString = Sys.Serialization.JavaScriptSerializer.serialize(json);
   
var currsArea=document.getElementById("Text1");
    
var currsText = document.getElementById("lbl_JsonString");
    currsText.value 
= jsonString;    
     
    
for(var i=0;i<exampleArray.length;i++)
    
{
        
var group = document.createElement("li");
        group.innerHTML 
= exampleArray[i];
        currsArea.appendChild(group);
    }

        
}


服务端:
  public   class  JsonObject
        
{
            
public string value = "0";
            
public string text = String.Empty;
        }

        
protected   void  ServerButton2_Click( object  sender, EventArgs e)
        
{
            System.Web.Script.Serialization.JavaScriptSerializer mySerializer 
= new System.Web.Script.Serialization.JavaScriptSerializer();

            JsonObject json 
= mySerializer.Deserialize<JsonObject>(this.lbl_JsonString.Text);

            
this.Label2.Text = json.text.ToString();
        }

页面

< div  id ="quickSummary" >
< class ="p1"  id ="Text1" > &nbsp;&nbsp;
    
    
    
</ p >
< input  id ="CilentButton1"  type ="button"  value ="CilentButton"  onclick ="javaScript:Show();"   />
    
< asp:ScriptManager  ID ="ScriptManager1"   runat ="server" >
    
</ asp:ScriptManager >
    
    
&nbsp; < aspx:UpdatePanel  ID ="UpdatePanel1"  runat ="server" >
        
< ContentTemplate >
           
            
< asp:Label  ID ="Label2"  runat ="server"  Text ="Label" ></ asp:Label >< br />
           
            
< asp:Label  ID ="Label3"  runat ="server"  Text ="JSON" ></ asp:Label >< asp:TextBox  ID ="lbl_JsonString"  runat ="server" ></ asp:TextBox >
           
            
< asp:Button  ID ="ServerButton2"
        runat
="server"  Text ="ServerButton"  
    onclick
="ServerButton2_Click"   />
        
</ ContentTemplate >
       
        
< Triggers >
            
< aspx:AsyncPostBackTrigger  ControlID ="ServerButton2"  EventName ="Click"   />
        
</ Triggers >
       
    
</ aspx:UpdatePanel >
</ div >
< div  id ="ShowSource1" >
</ div >


其实比较简单,但我想要用LINQ to JavaScript,要这样才完整嘛.

worksguo

欢迎评论
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值