• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
works guo

成功的渴望与生俱来---- 成功是获得赞美与尊重的最有效的途径
博客园    首页    新随笔    联系   管理    订阅  订阅

LINQ to JavaScript

LINQ to JavaScript(JSLINQ)是一个LINQ to Object在 javaScript中的实现,它对JavaScript Arrary Objcet构建一些列的扩展方法.如果你使用一个Array,你能使用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">
<p 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

欢迎评论
posted @ 2008-04-25 22:27  works guo  阅读(1218)  评论(7)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3