ASP.NET List<>中Sort()、Find()、FindAll()、Exist()的使用方法

[C#/ASP.NET]中List<T>真的非常好用。一个例子简单说明一下List<>中Sort()、Find()、FindAll()、Exist()的使用方法

简单介绍:

List<T>.Sort() → 排序T

List<T>.Find() → 找出一個T

List<T>.FindAll() →找出多個T

List<T>.Exist() →判斷T是否存在

 示例代码:

 页面文件GenericList.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GenericList.aspx.cs" Inherits="GenericList" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>GenericList</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        原始資料:
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </div>
    </form>
</body>
</html>

后台代码文件GenericList.aspx.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
public partial class GenericList : System.Web.UI.Page
{
 
    protected void Page_Load(object sender, EventArgs e)
    {
        List<Person> lstPerson = new List<Person>();
        lstPerson.Add(new Person(1, "puma", 10));
        lstPerson.Add(new Person(2, "F6 Team", 20));
        lstPerson.Add(new Person(3, "ASP.NET", 30));
        lstPerson.Add(new Person(4, "Dotblogs", 40));
 
        //原始資料顯示在GridView上
        this.GridView1.DataSource = lstPerson;
        this.GridView1.DataBind();
 
 
 
        //List<T>.Find()
        //找出Name='puma'的Person
        Response.Write("找出Name='puma'的Person→ ");
        Response.Write(lstPerson.Find(delegate(Person p) { return p.Name == "puma"; }).ToString() + "<p>");
 
 
 
        //List<T>.FindAll()
        //找出Age>10的數目
        Response.Write("找出Age>10的數目→ ");
        Response.Write(lstPerson.FindAll(delegate(Person p) { return p.Age > 10; }).Count.ToString() + "<p>");
 
 
 
        //List<T>.Exists()
        //檢查Name='F6'是否存在
        Response.Write("檢查Name='F6'是否存在→ ");
        Response.Write(lstPerson.Exists(delegate(Person p) { return p.Name == "F6"; }).ToString() + "<p>");
 
 
 
        //List<T>.Sort()
        //依Name升冪排序
        Response.Write("<p>依Name升冪排序↑<br/>");
        lstPerson.Sort(delegate(Person p1, Person p2) { return Comparer<string>.Default.Compare(p1.Name, p2.Name); });
        foreach (Person p in lstPerson)
        {
            Response.Write(p.ToString() + "<br/>");
        }
 
 
 
        //List<T>.Sort()
        //依Name降冪排序
        Response.Write("<p>依Name降冪排序↓<br/>");
        lstPerson.Sort(delegate(Person p1, Person p2) { return Comparer<string>.Default.Compare(p2.Name, p1.Name); });
        foreach (Person p in lstPerson)
        {
            Response.Write(p.ToString() + "<br/>");
        }
    }
}
 
public class Person
{
    private int _ID;
    private string _Name;
    private int _Age;
 
    public Person(int ID, string Name, int Age)
    {
        _ID = ID;
        _Name = Name;
        _Age = Age;
    }
 
    public int ID
    {
        set { _ID = value; }
        get { return _ID; }
    }
 
    public string Name
    {
        set { _Name = value; }
        get { return _Name; }
    }
 
    public int Age
    {
        set { _Age = value; }
        get { return _Age; }
    }
 
    public override string ToString()
    {
        return string.Format("ID:{0},Name:{1},Age:{2}", _ID, _Name, _Age);
    }
}

  

posted @ 2013-04-29 15:46  AIの海雅  阅读(639)  评论(0编辑  收藏  举报