在ASP.NET中使用LINQ

2014 年 11 月 3 日3220

注意,除了可以使用上面的GridView控件外,我也可以使用 <asp:repeater>, <asp:datalist>, <asp:dropdownlist>, 或者任何其他ASP.NET的列表控件(可以是产品自带或者开发人员自己开发的控件)。在这些示例中我只使用了<asp:gridview>-但是你们可以使用任何其他的控件。

第二步:使用功能更丰富的集合

搜索一个数组的字符串并没多大意思,虽然有时候很有用。如果我们能对自己的功能更丰富的那些集合中搜索将 会更有趣。好消息是,LINQ使这些变得很简单。例如,为了更好记录我去过的地方,我在我的工程中建立了一 个叫"Location"的简单类:





using System;



Location



{



// Fields _country;



_distance;



_city;



// Properties Country



{



_country; }



value; }



}



Distance



{



_distance; }



value; }



}



City



{



_city; }



value; }



}



}





它公开了三个属性来表示国家、城市名称和到西雅图的距离。然后我新建一个包含GridView控件的Step3.aspx页 面,其中GridView定义了三列,如下所示:

然后我建立一个Location对象集合来绑定到Grid中,后台代码文件如下所示:





using System;



using System.Collections.Generic;



using System.Web;



using System.Query;



Step2 : System.Web.UI.Page



{



sender, EventArgs e)



{



List{



},



},



},



},



},



},



},



},



},



},



}



& nbsp; };



GridView1.DataSource cities



& nbsp; orderby location.Country, location.City



& nbsp; select location;



GridView1.DataBind();



}



}





1

0 0