Linq City集合描述
创始人
2024-06-06 18:11:18
0

稍微重构一下Linq City集合

因为我们将在好几个示例中重用这个Linq City集合,我决定把它封装到一个"TravelOrganizer"类中,如下所示:

  1. using System;  
  2. using System.Collections.Generic;   
  3. public class TravelOrganizer  
  4. {  
  5. public List PlacesVisited  
  6. {  
  7. get  
  8. {  
  9. List cities = new List{  
  10. & nbsp; & nbsp;new Location { City="London"Distance=4789Country="UK" },  
  11. & nbsp; & nbsp;new Location { City="Amsterdam"Distance=4869Country="Netherlands" },  
  12. & nbsp; & nbsp;new Location { City="San Francisco"Distance=684Country="USA" },  
  13. & nbsp; & nbsp;new Location { City="Las Vegas"Distance=872Country="USA" },  
  14. & nbsp; & nbsp;new Location { City="Boston"Distance=2488Country="USA" },  
  15. & nbsp; & nbsp;new Location { City="Raleigh"Distance=2363Country="USA" },  
  16. & nbsp; & nbsp;new Location { City="Chicago"Distance=1733Country="USA" },  
  17. & nbsp; & nbsp;new Location { City="Charleston"Distance=2421Country="USA" },  
  18. & nbsp; & nbsp;new Location { City="Helsinki"Distance=4771Country="Finland" },  
  19. & nbsp;new Location { City="Nice"Distance=5428Country="France" },  
  20. & nbsp; & nbsp;new Location { City="Dublin"Distance=4527Country="Ireland" }  
  21. & nbsp; & nbsp;};  
  22.    
  23. return cities;  
  24. }  
  25. }  

这使我只需要编写如下的代码就能得到跟上面同样的结果:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web;  
  4. using System.Web.UI;  
  5. using System.Query;  
  6.    
  7. public partial class Step3 : System.Web.UI.Page  
  8. {  
  9. protected void Page_Load(object sender, EventArgs e)  
  10. {  
  11. TravelOrganizer travel = new TravelOrganizer();  
  12.    
  13. GridView1.DataSource = from location in travel.PlacesVisited  
  14. & nbsp; where location.Distance > 1000  
  15. & nbsp; orderby location.Country, location.City  
  16. & nbsp; select location;  
  17.    
  18. GridView1.DataBind();  
  19. }  

LINQ很酷之处就是它是强类型的。这意味着:

1) 你的所有的查询都会进行编译时检查。不像现在的SQL语句,你只有到运行时才会发现你的错误所 在。这意味着你在开发时就可以检查你的代码的正确性,例如,如果我把上面的"distance"误写成 了"distanse",编译器将为我捕获到这个错误。

2) 当你写LINQ查询的时候你将在VS或免费的Visual Web Developer中获得智能感知的提示。这不仅加 快了编码的输入速度,而且使我们在处理无论简单还是复杂的集合和数据源对象模型时都变得非常容易。以上介绍Linq City集合

【编辑推荐】

  1. LINQ to SQL Table浅谈
  2. Linq语句问题的解决方法
  3. Ling to sql更新实体概述
  4. Linq实体继承简单描述
  5. Linq Library概述

相关内容

热门资讯

如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...