LINQ簡明教程:數據排序、分組、過濾

jopen 10年前發布 | 28K 次閱讀 LINQ .NET開發

引言

現在網絡上有很多關于LINQ的使用教程,所以我并不想證明這篇文章有多么的與眾不同,或許有不少前輩們已經走在了我的前面。但是我發現LINQ真的非常有趣,忍不住想分享一下關于LINQ的使用知識,這些都是我自己的實踐經驗總結,希望可以幫助正在學習LINQ的同學。

背景

LINQ可以對很多數據源進行查詢操作,比如數據庫、數組(array)、鏈表(list)、XML文件等。在本文中,我將從數組中提取數據,這些數據是10個最受歡迎的國家。有一個類叫Countries,有countrypopulation and continent這些屬性。我們將以Countries類為元素的數組作為數據源,綁定到GridView進行顯示,并且利用LINQ對數據進行排序、分組和過濾。

下面是一些效果圖:

LINQ簡明教程:數據排序、分組、過濾

LINQ簡明教程:數據排序、分組、過濾

LINQ簡明教程:數據排序、分組、過濾

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提供了很多簡單而高效的方式操作數據源,這個示例我使用Visual Studio Express 2013開發的。點擊這里可以下載整個工程的源代碼。

來自:http://www.codeceo.com/article/linq-usage-sort-group-filter.html

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!