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

下面是一些效果图:

代码

下面就是Countries类:

public class Countries
{
public string Country
{
get;
set;
}
public long Population
{
get;
set;
}
public string Continent
{
get;
set;
}
}

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

protected void Page_Load(object sender, EventArgs e)
{
for (int ctr = 0; ctr < cc.Length;ctr++ )
{
cc[ctr] = new Countries();
}
cc[0].Country = "Bangladesh";
cc[0].Population = 156594962;
cc[0].Continent = "Asia";
cc[1].Country = "Brazil";
cc[1].Population = 200361925;
cc[1].Continent = "America";
cc[2].Country = "China";
cc[2].Population = 1357380000;
cc[2].Continent = "Asia";
cc[3].Country = "India";
cc[3].Population = 1252139596;
cc[3].Continent = "Asia";
cc[4].Country = "Indonesia";
cc[4].Population = 249865631;
cc[4].Continent = "Asia";
cc[5].Country = "Japan";
cc[5].Population = 127338621;
cc[5].Continent = "Asia";
cc[6].Country = "Nigeria";
cc[6].Population = 173615345;
cc[6].Continent = "Africa";
cc[7].Country = "Pakistan";
cc[7].Population = 182142594;
cc[7].Continent = "Asia";
cc[8].Country = "Russian Federation";
cc[8].Population = 143499861;
cc[8].Continent = "Europe";
cc[9].Country = "United States";
cc[9].Population = 316128839;
cc[9].Continent = "America";
btnDisplay_Click(sender, e);
}

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

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

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

protected void btnAsc_Click(object sender, EventArgs e)
{
Label2.Text = "In Ascending Order of Population";
var info = from i in cc orderby i.Population select i;
GridView1.DataSource = info;
GridView1.DataBind();
}
protected void btnDesc_Click(object sender, EventArgs e)
{
Label2.Text = "In Descending Order of Population";
var info = from i in cc orderby i.Population descending select i;
GridView1.DataSource = info;
GridView1.DataBind();
}

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

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

protected void btnGroup_Click(object sender, EventArgs e)
{
Label2.Text = "Continent Wise Group Data";
var info = from i in cc
orderby i.Continent
group i by i.Continent into g
select new
{
Continent = g.Key,
NumberOfCountries = g.Count(),
TotalPopulation = g.Sum(s => s.Population),
AveragePopulation = g.Average(a => a.Population)
};
GridView1.DataSource = info;
GridView1.DataBind();
}

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

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

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

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. The formatter threw an exception while trying to deserialize the message in WCF

    有一个WCF应用, 主要功能是存储doc, txt等类型文件到database,当文件的大小在16kb之内,调用WCF service能正常工作:但如果文件大小超出16KB之外, 它将抛出这样一个错误 ...

  2. The underlying provider failed on Open. EF

    本地测试是可以的:但是放到服务器上就不行了: 报错:"The underlying provider failed on Open." 这一情况和我以前遇上的一次错误有点相似啊:都 ...

  3. spring 日志

    spring日志结构

  4. Red Hat TimesTen安装记录

    1:内核参数修改 # vi /etc/sysctl.conf kernel.sem= #sysctl –p 备注:此安装过程为测试环境,具体参数修改要参考TimesTen官方文档. 2:创建用户及组信 ...

  5. UOJ 216 Jakarta Skyscrapers

    http://uoj.ac/problem/216 题意:给定A,B,C,如果集合中有数i,j(i>j),那么集合就会增加i-j这个数,问有没有在初始集合为{A,B}400步内生成C的方案. 思 ...

  6. 主流屏幕对比:IPS/LTPS/CGS/IGZO/AMOLED

    IPS.LTPS.CGS.IGZO.AMOLED都是什么屏幕又有什么区别?目前的手机屏幕技术实在太多,本文旨在介绍各种面板以及屏幕技术,便于大家更好地进行区分. 近年来手机屏幕技术层出不穷,早在几年前 ...

  7. oracle_执行计划_谓词信息和数据获取(access and filter区别) (转)

    These two terms in the Predicate Information section indicate when the data source is reduced. Simpl ...

  8. JAVA首选五款开源Web开发框架

    Spring Spring是一个开源的Java/Java EE全功能栈应用程序框架,在JavaEE社区中非常受欢迎,以Apache许可证形式发布,也有.NET平台上的移植版本. Struts2 Str ...

  9. MFC自定义消息

    本文地址:http://blog.163.com/strive_only/blog/static/893801682010101911467765/ 消息机制是windows的典型运行机制,在MFC中 ...

  10. cryptopp开源库的使用(零):windows下使用visual studio编译

    编译相当简单:打开目录下的sln文件直接编译即可,官方支持到vc2012,我使用vs2013也没有错误,优秀的开源库总是便于使用. 编译的时候注意运行库得选择需要跟使用该库的保持一致,否则会出现重定义 ...