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. javascript特殊运算符(in,instanceof,typeof,delete,void,逗号)

    in运算符                 in运算符要求其左边的运算数是一个字符串,或可以被转换为字符串,右边的运算数十一个对象或数组.如果该 运算符左边的值是右边对象的一个属性名,则返回true, ...

  2. Yii框架AR对象数据转化为数组

    demo函数作用:将AR对象数据转化为数组 局限:仅用于findAll的多维数组,find一维数组可以先转化为多维数组的一个元素在使用 function actionIndex() { $data = ...

  3. SCJP_104——题目分析(5)

    18. public class Test { public static void add3(Integer i) { int val=i.intvalue(); val+=3; i=new Int ...

  4. codeforces 232D Fence

    John Doe has a crooked fence, consisting of n rectangular planks, lined up from the left to the righ ...

  5. PHP之路——VC库

    VC库:https://pan.baidu.com/s/1dF9LslV    密码:v7ap

  6. cypress的EZ-USB对于USB的介绍

    Host is MasterThis is a fundamental USB concept. There is exactly onemaster in a USB system: the hos ...

  7. hdu How many integers can you find

    题意:找出小于n是m个数每个数的倍数的数的个数. 思路:用二进制表示是那几个数的倍数. 二进制进行容斥,去掉小于0的数. #include <cstdio> #include <cs ...

  8. POJ2503 Babelfish

    题目链接. 分析: 应当用字典树,但stl的map做很简单. #include <iostream> #include <cstdio> #include <cstdli ...

  9. yui--datatable基础和常用知识总结

    1.namespace 用于创建一个全局的命名空间,使用YUI时,首先会自动创建widget,util,example三个命名空间,使用时也可以自定义命名空间.类似于在程序中建了了一个static变量 ...

  10. 开源项目AndroidUtil-采用Fragment实现TabHost

    原文出自:方杰|http://fangjie.info/?p=141 转载请注明出处 学习Android也有一段时间了,感觉大部分的Android应用都有很多类似的组件,所以就打算做了这样一个开源项目 ...