Linq .NET查询操作浅析
创始人
2024-06-06 18:01:05
0

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

Linq .NET查询操作

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

  1. <%@ Page Language="C#" CodeFile="Step5.aspx.cs" Inherits="Step5" %> 
  2.    
  3.  
  4.  
  5.  id="form1" runat="server"> 
  6.  
  7. Aggregate Value Samples

     
  8.    
  9.  
  10. Farthest Distance City: 
  11.  ID="MaxCityNameTxt" runat="server" Text="Label"> 
  12.  ID="MaxCityDistanceTxt" runat="server" Text="Label"> 
 
  •    
  •  
  • Total Travel Distance (outside of US): 
  •  ID="TotalDistanceTxt" runat="server" Text="Label"> 
  •    
  •    
  •  
  • Average Distance: 
  •  ID="AverageDistanceTxt" runat="server" Text="Label"> 
  •    
  •    
  •  
  •  
  •  
  •  
  • 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概述

    相关内容

    热门资讯

    如何允许远程连接到MySQL数... [[277004]]【51CTO.com快译】默认情况下,MySQL服务器仅侦听来自localhos...
    如何利用交换机和端口设置来管理... 在网络管理中,总是有些人让管理员头疼。下面我们就将介绍一下一个网管员利用交换机以及端口设置等来进行D...
    施耐德电气数据中心整体解决方案... 近日,全球能效管理专家施耐德电气正式启动大型体验活动“能效中国行——2012卡车巡展”,作为该活动的...
    20个非常棒的扁平设计免费资源 Apple设备的平面图标PSD免费平板UI 平板UI套件24平图标Freen平板UI套件PSD径向平...
    德国电信门户网站可实时显示全球... 德国电信周三推出一个门户网站,直观地实时提供其安装在全球各地的传感器网络检测到的网络攻击状况。该网站...
    为啥国人偏爱 Mybatis,... 关于 SQL 和 ORM 的争论,永远都不会终止,我也一直在思考这个问题。昨天又跟群里的小伙伴进行...
    《非诚勿扰》红人闫凤娇被曝厕所... 【51CTO.com 综合消息360安全专家提醒说,“闫凤娇”、“非诚勿扰”已经被黑客盯上成为了“木...
    2012年第四季度互联网状况报... [[71653]]  北京时间4月25日消息,据国外媒体报道,全球知名的云平台公司Akamai Te...