非常简单的 LINQ 示例






3.40/5 (9投票s)
这是一个非常简单但实用的 LINQ 实现。
引言
在网络上以及 CodeProject 上有很多优秀的、非常有用的 LINQ 文章。因此,我并不声称这篇文章展示了什么非凡的东西,或者以前从未尝试过的事情。但我发现 LINQ 非常有趣,我想分享一些我自己实现并可以帮助那些想学习 LINQ 的人。
在本文中,我将展示 LINQ 在显示、排序、分组和过滤数据方面的应用。
背景
LINQ 可以用于对各种数据源执行查询操作,例如数据库、数组、列表、XML 文件等。在本文中,我提取存储在数组中的数据。这些数据是关于世界上人口最多的 10 个国家。有一个名为 Countries
的类,具有 country
(国家)、population
(人口)和 continent
(大陆)属性。使用 Countries
类的数组来保存关于世界上人口最多的 10 个国家的数据。使用 LINQ 提取这些数据并绑定到 GridView
对象。
注意:为了分组的目的,我将美国和巴西的大陆指定为美洲,而不是北美和南美。
Using the Code
以下是 Countries
类的代码
public class Countries
{
public string Country
{
get;
set;
}
public long Population
{
get;
set;
}
public string Continent
{
get;
set;
}
}
以下是 Page_Load
事件处理程序,它将 countries
信息存储在 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);
}
以下是“显示”按钮的单击事件代码,用于显示按字母顺序排列的国家/地区列表
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();
}
以上代码从数组中获取数据并将其绑定到 GridView
。
以下代码用于按升序和降序对数据进行排序
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
的升序或降序对数据进行排序。
以下代码根据 continent
对 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()
等)生成分组结果。
以下代码用于根据在文本框中指定国家/地区名称过滤数据
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 提供了一种简单而有效的方法来处理不同的数据源。
我使用 Microsoft Visual Studio Express 2013 for Web 来开发这个 Web 应用程序。