自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Linq .NET查詢操作淺析

開發(fā) 后端
這里介紹你可以用之來建立你自己的Linq .NET查詢操作,所有的對象都是強(qiáng)類型的,而且支持智能感知和編譯時(shí)檢查。

本文向大家介紹Linq .NET查詢操作,可能好多人還不了解Linq .NET查詢操作,沒有關(guān)系,看完本文你肯定有不少收獲,希望本文能教會(huì)你更多東西。

Linq .NET查詢操作

除了可以返回?cái)?shù)據(jù)集之外,我們可以使用Linq .NET查詢操作來返回單個(gè)或者統(tǒng)計(jì)數(shù)據(jù)結(jié)果。下面的例子演示了怎么做:

  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后臺(tái)代碼文件:

  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. }  

注意,上面最后兩個(gè)例子使用了新的Lambda表達(dá)式(Lambda Expression)支持-這些表達(dá)式允許我們通過譬如象委托這樣的代碼段在數(shù)據(jù)之上做進(jìn)一步的操作,從而計(jì)算出一個(gè)結(jié)果來。你也可以用之來建立你自己的Linq .NET查詢操作(例如:你可以建立一些特定領(lǐng)域的查詢來計(jì)算運(yùn)費(fèi)或者收入稅)。所有的對象都是強(qiáng)類型的,而且支 持智能感知和編譯時(shí)檢查。

【編輯推薦】

  1. LINQ to SQL Table淺談
  2. Linq語句問題的解決方法
  3. Ling to sql更新實(shí)體概述
  4. Linq實(shí)體繼承簡單描述
  5. Linq Library概述
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-09-15 17:16:58

LINQ查詢操作符

2009-09-15 13:30:54

linq級聯(lián)

2009-09-14 13:37:25

LINQ ADO.NE

2009-09-14 18:23:59

LINQ嵌套查詢

2009-09-17 18:05:15

linq to sql

2009-09-15 09:19:22

linq動(dòng)態(tài)條件

2009-09-15 10:35:11

linq多表查詢

2009-09-14 17:10:57

LINQ模糊查詢

2009-09-14 19:14:51

LINQ動(dòng)態(tài)查詢

2009-09-14 10:13:02

LINQ查詢操作

2009-09-08 16:36:10

LINQ查詢基于泛型類

2009-09-13 21:52:16

LINQ字符串

2009-09-16 17:29:10

Linq查詢二維數(shù)組

2009-09-16 10:48:32

LINQ查詢操作

2009-09-15 14:30:11

Linq連接

2009-09-14 18:19:49

LINQ模糊查詢

2009-09-17 09:09:50

Lambda表達(dá)式Linq查詢

2009-09-08 10:57:55

LINQ查詢操作

2009-09-11 13:29:31

LINQ查詢操作

2009-09-14 18:57:19

LINQ查詢
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號