温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Linq 中如何使用City集合

发布时间:2021-07-20 11:16:33 来源:亿速云 阅读:111 作者:Leah 栏目:编程语言

Linq 中如何使用City集合,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

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

using System;  using System.Collections.Generic;   public class TravelOrganizer  {  public List<Location> PlacesVisited  {  get  {  List<Location> cities = new List<Location>{  & 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;  }  }  }

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

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很酷之处就是它是强类型的。这意味着:

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

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

看完上述内容,你们掌握Linq 中如何使用City集合的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI