来源: [C#/ASP.NET]List中Sort()、Find()、FindAll()、Exist()的使用方法 – zock – 博客园
[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
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(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); } } |
本文地址:http://www.cnblogs.com/atree/archive/2011/02/25/Asp-Net_List_Sort_Find_FindAll_Exist.html