新聞中心
稍微重構(gòu)一下Linq City集合

創(chuàng)新互聯(lián)公司主要從事網(wǎng)頁(yè)設(shè)計(jì)、PC網(wǎng)站建設(shè)(電腦版網(wǎng)站建設(shè))、wap網(wǎng)站建設(shè)(手機(jī)版網(wǎng)站建設(shè))、響應(yīng)式網(wǎng)站設(shè)計(jì)、程序開(kāi)發(fā)、網(wǎng)站優(yōu)化、微網(wǎng)站、微信平臺(tái)小程序開(kāi)發(fā)等,憑借多年來(lái)在互聯(lián)網(wǎng)的打拼,我們?cè)诨ヂ?lián)網(wǎng)網(wǎng)站建設(shè)行業(yè)積累了豐富的網(wǎng)站建設(shè)、網(wǎng)站制作、網(wǎng)站設(shè)計(jì)、網(wǎng)絡(luò)營(yíng)銷經(jīng)驗(yàn),集策劃、開(kāi)發(fā)、設(shè)計(jì)、營(yíng)銷、管理等多方位專業(yè)化運(yùn)作于一體。
因?yàn)槲覀儗⒃诤脦讉€(gè)示例中重用這個(gè)Linq City集合,我決定把它封裝到一個(gè)"TravelOrganizer"類中,如下所示:
- using System;
- using System.Collections.Generic;
- public class TravelOrganizer
- {
- public List
PlacesVisited - {
- get
- {
- List
cities = new List { - & nbsp; & nbsp;new Location { City="London", Distance=4789, Country="UK" },
- & nbsp; & nbsp;new Location { City="Amsterdam", Distance=4869, Country="Netherlands" },
- & nbsp; & nbsp;new Location { City="San Francisco", Distance=684, Country="USA" },
- & nbsp; & nbsp;new Location { City="Las Vegas", Distance=872, Country="USA" },
- & nbsp; & nbsp;new Location { City="Boston", Distance=2488, Country="USA" },
- & nbsp; & nbsp;new Location { City="Raleigh", Distance=2363, Country="USA" },
- & nbsp; & nbsp;new Location { City="Chicago", Distance=1733, Country="USA" },
- & nbsp; & nbsp;new Location { City="Charleston", Distance=2421, Country="USA" },
- & nbsp; & nbsp;new Location { City="Helsinki", Distance=4771, Country="Finland" },
- & nbsp;new Location { City="Nice", Distance=5428, Country="France" },
- & nbsp; & nbsp;new Location { City="Dublin", Distance=4527, Country="Ireland" }
- & nbsp; & nbsp;};
- return cities;
- }
- }
- }
這使我只需要編寫(xiě)如下的代碼就能得到跟上面同樣的結(jié)果:
- using System;
- using System.Collections.Generic;
- using System.Web;
- using System.Web.UI;
- using System.Query;
- public partial class Step3 : System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- TravelOrganizer travel = new TravelOrganizer();
- GridView1.DataSource = from location in travel.PlacesVisited
- & nbsp; where location.Distance > 1000
- & nbsp; orderby location.Country, location.City
- & nbsp; select location;
- GridView1.DataBind();
- }
- }
LINQ很酷之處就是它是強(qiáng)類型的。這意味著:
1) 你的所有的查詢都會(huì)進(jìn)行編譯時(shí)檢查。不像現(xiàn)在的SQL語(yǔ)句,你只有到運(yùn)行時(shí)才會(huì)發(fā)現(xiàn)你的錯(cuò)誤所 在。這意味著你在開(kāi)發(fā)時(shí)就可以檢查你的代碼的正確性,例如,如果我把上面的"distance"誤寫(xiě)成 了"distanse",編譯器將為我捕獲到這個(gè)錯(cuò)誤。
2) 當(dāng)你寫(xiě)LINQ查詢的時(shí)候你將在VS或免費(fèi)的Visual Web Developer中獲得智能感知的提示。這不僅加 快了編碼的輸入速度,而且使我們?cè)谔幚頍o(wú)論簡(jiǎn)單還是復(fù)雜的集合和數(shù)據(jù)源對(duì)象模型時(shí)都變得非常容易。以上介紹Linq City集合
當(dāng)前文章:LinqCity集合描述
文章地址:http://fisionsoft.com.cn/article/cddcgde.html


咨詢
建站咨詢
