Linq 中如何使用City集合,相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。因为我们将在好几个示例中重用这个Linq City集合,我决定把它封装到一个"Trave
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集合的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注编程网精选频道,感谢各位的阅读!
--结束END--
本文标题: Linq 中如何使用City集合
本文链接: https://www.lsjlt.com/news/293665.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
下载Word文档到电脑,方便收藏和打印~
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
2024-05-16
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0