Linq .NET查询操作浅析

开发 后端
这里介绍你可以用之来建立你自己的Linq .NET查询操作,所有的对象都是强类型的,而且支持智能感知和编译时检查。

本文向大家介绍Linq .NET查询操作,可能好多人还不了解Linq .NET查询操作,没有关系,看完本文你肯定有不少收获,希望本文能教会你更多东西。

Linq .NET查询操作

除了可以返回数据集之外,我们可以使用Linq .NET查询操作来返回单个或者统计数据结果。下面的例子演示了怎么做:

  1. <%@ Page Language="C#" CodeFile="Step5.aspx.cs" Inherits="Step5" %> 
  2.    
  3. <html> 
  4. <body> 
  5. <form id="form1" runat="server"> 
  6. <div> 
  7. <h1>Aggregate Value Samples</h1> 
  8.    
  9. <div> 
  10. <b>Farthest Distance City:</b> 
  11. <asp:Label ID="MaxCityNameTxt" runat="server" Text="Label"></asp:Label> 
  12. <asp:Label ID="MaxCityDistanceTxt" runat="server" Text="Label"></asp:Label> 
  13. </div> 
  14.    
  15. <div> 
  16. <b>Total Travel Distance (outside of US):</b> 
  17. <asp:Label ID="TotalDistanceTxt" runat="server" Text="Label"></asp:Label> 
  18. </div>   
  19.    
  20. <div> 
  21. <b>Average Distance:</b> 
  22. <asp:Label ID="AverageDistanceTxt" runat="server" Text="Label"></asp:Label> 
  23. </div>   
  24.    
  25. </div> 
  26. </form> 
  27. </body> 
  28. </html> 

Step5.aspx.cs后台代码文件:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Web.UI;  
  4. using System.Query;  
  5.    
  6. public partial class Step5 : System.Web.UI.Page  
  7. {  
  8. protected void Page_Load(object sender, EventArgs e)  
  9. {  
  10. TravelOrganizer travel = new TravelOrganizer();  
  11.  
  12. // Calculate farthest city away  
  13.  
  14. Location farthestCity = (from location in travel.PlacesVisited  
  15. & nbsp; & nbsp; orderby location.Distance descending  
  16. & nbsp; & nbsp; select location).First();  
  17.    
  18. MaxCityNameTxt.Text = farthestCity.City;  
  19. MaxCityDistanceTxt.Text = "(" + farthestCity.Distance + " miles)";  
  20.  
  21. // Calculate total city distances of all cities outside US  
  22.    
  23. int totalDistance = (from location in travel.PlacesVisited  
  24. & nbsp; where location.Country != "USA"  
  25. & nbsp; select location).Sum(loc => loc.Distance);  
  26.    
  27. TotalDistanceTxt.Text = totalDistance + " miles";  
  28.  
  29. // Calculate average city distances of each city trip  
  30.    
  31. double averageDistance = travel.PlacesVisited.Average(loc => loc.Distance);  
  32.    
  33. AverageDistanceTxt.Text = averageDistance + " miles";  
  34. }  

注意,上面最后两个例子使用了新的Lambda表达式(Lambda Expression)支持-这些表达式允许我们通过譬如象委托这样的代码段在数据之上做进一步的操作,从而计算出一个结果来。你也可以用之来建立你自己的Linq .NET查询操作(例如:你可以建立一些特定领域的查询来计算运费或者收入税)。所有的对象都是强类型的,而且支 持智能感知和编译时检查。

【编辑推荐】

  1. LINQ to SQL Table浅谈
  2. Linq语句问题的解决方法
  3. Ling to sql更新实体概述
  4. Linq实体继承简单描述
  5. Linq Library概述
责任编辑:佚名 来源: 博客园
相关推荐

2009-09-15 17:16:58

LINQ查询操作符

2009-09-15 13:30:54

linq级联

2009-09-14 13:37:25

LINQ ADO.NE

2009-09-15 09:19:22

linq动态条件

2009-09-17 18:05:15

linq to sql

2009-09-14 18:23:59

LINQ嵌套查询

2009-09-14 19:14:51

LINQ动态查询

2009-09-14 17:10:57

LINQ模糊查询

2009-09-15 10:35:11

linq多表查询

2009-09-14 10:13:02

LINQ查询操作

2009-09-13 21:52:16

LINQ字符串

2009-09-16 17:29:10

Linq查询二维数组

2009-09-08 16:36:10

LINQ查询基于泛型类

2009-09-16 10:48:32

LINQ查询操作

2009-09-17 09:09:50

Lambda表达式Linq查询

2009-09-14 18:19:49

LINQ模糊查询

2009-09-15 14:30:11

Linq连接

2009-09-11 13:29:31

LINQ查询操作

2009-09-08 10:57:55

LINQ查询操作

2009-09-14 18:57:19

LINQ查询
点赞
收藏

51CTO技术栈公众号