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

来源:互联网 发布:云端瓷砖进销存软件 编辑:程序博客网 时间:2024/05/23 13:37

简单介绍:

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

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

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

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

 示例代码:

 页面文件GenericList.aspx

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<%@ 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

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
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);
    }
}
原创粉丝点击