LINQ可以对很多数据源进行查询操作,比如数据库、数组(array)、链表(list)、XML文件等。在本文中,我将从数组中提取数据,这些数据是10个最受欢迎的国家。有一个类叫Countries,有countrypopulation and continent这些属性。我们将以Countries类为元素的数组作为数据源,绑定到GridView进行显示,并且利用LINQ对数据进行排序、分组和过滤。

下面是一些效果图:

代码

下面就是Countries类:

  1. public class Countries
  2. {
  3. public string Country
  4. {
  5. get;
  6. set;
  7. }
  8. public long Population
  9. {
  10. get;
  11. set;
  12. }
  13. public string Continent
  14. {
  15. get;
  16. set;
  17. }
  18. }

下面的Page_Load函数将对 Countries类数组进行初始化:

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. for (int ctr = 0; ctr < cc.Length;ctr++ )
  4. {
  5. cc[ctr] = new Countries();
  6. }
  7. cc[0].Country = "Bangladesh";
  8. cc[0].Population = 156594962;
  9. cc[0].Continent = "Asia";
  10. cc[1].Country = "Brazil";
  11. cc[1].Population = 200361925;
  12. cc[1].Continent = "America";
  13. cc[2].Country = "China";
  14. cc[2].Population = 1357380000;
  15. cc[2].Continent = "Asia";
  16. cc[3].Country = "India";
  17. cc[3].Population = 1252139596;
  18. cc[3].Continent = "Asia";
  19. cc[4].Country = "Indonesia";
  20. cc[4].Population = 249865631;
  21. cc[4].Continent = "Asia";
  22. cc[5].Country = "Japan";
  23. cc[5].Population = 127338621;
  24. cc[5].Continent = "Asia";
  25. cc[6].Country = "Nigeria";
  26. cc[6].Population = 173615345;
  27. cc[6].Continent = "Africa";
  28. cc[7].Country = "Pakistan";
  29. cc[7].Population = 182142594;
  30. cc[7].Continent = "Asia";
  31. cc[8].Country = "Russian Federation";
  32. cc[8].Population = 143499861;
  33. cc[8].Continent = "Europe";
  34. cc[9].Country = "United States";
  35. cc[9].Population = 316128839;
  36. cc[9].Continent = "America";
  37. btnDisplay_Click(sender, e);
  38. }

点击展示按钮后,即可将数据绑定到GridView:

  1. protected void btnDisplay_Click(object sender, EventArgs e)
  2. {
  3. Label2.Text = "Alphabetical List";
  4. var info = from i in cc select i;
  5. GridView1.DataSource = info;
  6. GridView1.DataBind();
  7. }

下面的代码是利用LINQ对数据进行排序:

  1. protected void btnAsc_Click(object sender, EventArgs e)
  2. {
  3. Label2.Text = "In Ascending Order of Population";
  4. var info = from i in cc orderby i.Population select i;
  5. GridView1.DataSource = info;
  6. GridView1.DataBind();
  7. }
  8. protected void btnDesc_Click(object sender, EventArgs e)
  9. {
  10. Label2.Text = "In Descending Order of Population";
  11. var info = from i in cc orderby i.Population descending select i;
  12. GridView1.DataSource = info;
  13. GridView1.DataBind();
  14. }

正如你上面所看到的,orderby 可以指定按population属性正向排序还是反向排序。

下面的代码我们将利用LINQ实现按country分组的功能,并且列出国家的数量、总人口数以及平均人口数:

  1. protected void btnGroup_Click(object sender, EventArgs e)
  2. {
  3. Label2.Text = "Continent Wise Group Data";
  4. var info = from i in cc
  5. orderby i.Continent
  6. group i by i.Continent into g
  7. select new
  8. {
  9. Continent = g.Key,
  10. NumberOfCountries = g.Count(),
  11. TotalPopulation = g.Sum(s => s.Population),
  12. AveragePopulation = g.Average(a => a.Population)
  13. };
  14. GridView1.DataSource = info;
  15. GridView1.DataBind();
  16. }

groupby 操作可以实现不同的分组效果,我们可以根据 Count()Sum()Average()等方式来分组。

下面的代码将利用LINQ对数据进行过滤:

  1. protected void btnShow_Click(object sender, EventArgs e)
  2. {
  3. Label2.Text = "Data Filtered by Country Name";
  4. var info = from i in cc where i.Country.ToUpper() == txtCountry.Text.Trim().ToUpper() select i;
  5. GridView1.DataSource = info;
  6. GridView1.DataBind();
  7. }

where 操作可以指定数据过滤的条件,比如你可以根据国家名称来过滤。

LINQ简明教程:数据排序、分组、过滤的更多相关文章

  1. SQL语句检索数据排序及过滤

    阅读目录 一:排序检索数据 二:过滤数据 三:高级数据过滤 四:用通配符进行过滤 回到顶部 一:排序检索数据 1.1 排序数据 比如查询数据库中表数据的时候,我们使用如下语句: select * fr ...

  2. Linq 简明教程

    一个简单的实例 static void Main(string[] args) { string[] names = { "Alonso", "Zheng", ...

  3. 《mysql必知必会》笔记1(检索、排序、过滤、计算、汇聚、分组)

    一:了解SQL 1:列是表中的字段,所有表都由一个或多个列组成的.行是表中的记录,表中的数据都按行存储. 2:表中每一行都应该有可以唯一标识自己的一列或一组列.主键(一列或一组列),其值能够唯一区分每 ...

  4. SQL从入门到基础 - 04 SQLServer基础2(数据删除、数据检索、数据汇总、数据排序、通配符过滤、空值处理、多值匹配)

    一.数据删除 1. 删除表中全部数据:Delete from T_Person. 2. Delete 只是删除数据,表还在,和Drop Table(数据和表全部删除)不同. 3. Delete 也可以 ...

  5. 手把手教你如何用java8新特性将List中按指定属性排序,过滤重复数据

    在java中常常会遇到这样一个问题,在实际应用中,总会碰到对List排序并过滤重复的问题,如果List中放的只是简单的String类型过滤so easy,但是实际应用中并不会这么easy,往往List ...

  6. .NET LINQ 数据排序

    数据排序      排序操作按一个或多个特性对序列的元素进行排序. 第一个排序条件对元素执行主要排序. 通过指定第二个排序条件,可以对各个主要排序组中的元素进行排序.   方法 方法名 说明 C# 查 ...

  7. Java8简明教程(转载)

    ImportNew注:有兴趣第一时间学习Java 8的Java开发者,欢迎围观<征集参与Java 8原创系列文章作者>. 以下是<Java 8简明教程>的正文. “Java并没 ...

  8. Contoso 大学 - 3 - 排序、过滤及分页

    原文 Contoso 大学 - 3 - 排序.过滤及分页 目录 Contoso 大学 - 使用 EF Code First 创建 MVC 应用 原文地址:http://www.asp.net/mvc/ ...

  9. LINQ入门教程之各种标准查询操作符(一)

    好久之前就想系统的学习下LINQ,好久之前…… 本篇文章主要介绍LINQ等的标准查询操作符,内容取自<LINQ高级编程>,后续还会介绍LINQ to XML ,LINQ to SQL. L ...

随机推荐

  1. document.all和jq trigger原理

    document.all是页面内所有元素的一个集合.如:       document.all(0)表示页面内第一个元素document.all可以判断浏览器是否是IE     if(document ...

  2. Python爬虫预备知识

    1.http编程知识 http中client 和server的工作模式 client和server建立可靠的tcp链接(在HTTP1.1中这个链接是长时间的,超时断开策略) client通过socke ...

  3. Codeforces 556A Case of the Zeros and Ones(消除01)

    Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u   Description Andr ...

  4. Scut 进阶:网络模型拓扑

    处理消息流程: 关于是否能用 json 串作为 response? 在最后写消息的时候要加上控制选项,将Response类型,事直接以字节流,还是转json串再转字节流的方式进行编码了,如果要转jso ...

  5. COJ 0990 WZJ的数据结构(负十)

    WZJ的数据结构(负十) 难度级别:D: 运行时间限制:5000ms: 运行空间限制:51200KB: 代码长度限制:2000000B 试题描述 给你一个N个节点的有根树,从1到N编号,根节点为1并给 ...

  6. 【最长下降子序列】【动态规划】【二分】XMU 1041 Sequence

    题目链接: http://acm.xmu.edu.cn/JudgeOnline/problem.php?id=1041 题目大意: 一个二维平面,上面n(n<=1 000 000)个点.问至少选 ...

  7. Linux 下 的 cc 和 gcc

    在Linux下一会看到cc,另一会又看到gcc,感觉又点混乱的样子.它们是同一个东西么,有啥区别呢? 一分为二地看: 首先,如果讨论范围在Unix和Linux之间,那么cc和gcc不是同一个东西.cc ...

  8. 《Linear Algebra and Its Applications》-chaper2-矩阵的逆

    矩阵的逆: 逆矩阵的定义: 类比于我们在研究实数的时候回去讨论一个数的倒数,对应的,在矩阵运算中,当AB = I的时候,A,B互称为逆矩阵,这里的I类似实数中的1,表示单位矩阵,即对角线是1其余位置是 ...

  9. Java 8 中新的 Date 和 Time 类入门详解

    这篇文章主要是java8中新的Date和Time API的实战.新的Date和Time类是java开发者社区千呼万唤始出来的.Java8 之前存在的Date类一直都受人诟病,很多人都会选择使用第三方的 ...

  10. Back to Basics: Using KVO

    One of the things I like most about Apple’s iOS SDK is the consistent and easy-to-use API they provi ...