Linq City集合描述

开发 后端
这里介绍Linq City集合,包括介绍当你写LINQ查询的时候你将在VS或免费的Visual Web Developer中获得智能感知的提示。

稍微重构一下Linq City集合

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

  1. using System;  
  2. using System.Collections.Generic;   
  3. public class TravelOrganizer  
  4. {  
  5. public List<Location> PlacesVisited  
  6. {  
  7. get  
  8. {  
  9. List<Location> cities = new List<Location>{  
  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概述
责任编辑:佚名 来源: 博客园
相关推荐

2009-09-09 15:37:27

Linq DataLo

2009-09-09 15:28:43

Linq to obj

2009-09-18 16:00:07

LINQ架构

2009-09-14 10:57:46

LINQ入门

2009-09-15 16:26:36

Linq orderb

2009-09-10 10:37:15

LINQ to SQL

2009-09-16 09:38:27

LINQ To SQL

2009-09-14 15:43:12

Linq Settin

2009-09-09 11:14:04

Linq多个结果集

2009-09-18 13:53:09

LINQ工具集

2009-09-16 15:48:05

Linq修改XML文档

2009-09-14 14:58:52

LINQ to XML

2009-09-10 09:09:40

Linq实体继承

2009-09-14 13:14:49

LINQ序列

2009-09-17 17:14:54

linq to sql

2009-09-16 10:58:13

Linq数据分组

2009-09-14 16:33:55

LINQ To XML

2009-09-11 10:20:36

Linq扩展方法

2009-09-15 11:14:33

LINQ to SQL

2009-09-08 09:24:50

LINQ查询
点赞
收藏

51CTO技术栈公众号