利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題
2011-06-25 15:52
375 查看
List<T>.Sort() → 排序T
List<T>.Find() → 找出一個T
List<T>.FindAll() →找出多個T
List<T>.Exist() →判斷T是否存在
List<T>.Find() → 找出一個T
List<T>.FindAll() →找出多個T
List<T>.Exist() →判斷T是否存在
using System; |
002 | using System.Collections.Generic; |
003 | using System.Web; |
004 | using System.Web.UI; |
005 | using System.Web.UI.WebControls; |
006 |
007 | public partial class GenericList : System.Web.UI.Page |
008 | { |
009 |
010 | protected void Page_Load( object sender, EventArgs e) |
011 | { |
012 | List<Person> lstPerson= new List<Person>(); |
013 | lstPerson.Add( new Person(1, "puma" , 10)); |
014 | lstPerson.Add( new Person(2, "F6 Team" , 20)); |
015 | lstPerson.Add( new Person(3, "ASP.NET" , 30)); |
016 | lstPerson.Add( new Person(4, "Dotblogs" , 40)); |
017 |
018 | //原始資料顯示在GridView上 |
019 | this .GridView1.DataSource = lstPerson; |
020 | this .GridView1.DataBind(); |
021 |
022 |
023 |
024 | //List<T>.Find() |
025 | //找出Name='puma'的Person |
026 | Response.Write( "找出Name='puma'的Person→ " ); |
027 | Response.Write(lstPerson.Find( delegate (Personp) { return p.Name== "puma" ; }).ToString()+ "<p>" ); |
028 |
029 |
030 |
031 | //List<T>.FindAll() |
032 | //找出Age>10的數目 |
033 | Response.Write( "找出Age>10的數目→ " ); |
034 | Response.Write(lstPerson.FindAll( delegate (Personp) { return p.Age> 10; }).Count.ToString()+ "<p>" ); |
035 |
036 |
037 |
038 | //List<T>.Exists() |
039 | //檢查Name='F6'是否存在 |
040 | Response.Write( "檢查Name='F6'是否存在→ " ); |
041 | Response.Write(lstPerson.Exists( delegate (Personp) { return p.Name== "F6" ; }).ToString()+ "<p>" ); |
042 |
043 |
044 |
045 | //List<T>.Sort() |
046 | //依Name升冪排序 |
047 | Response.Write( "<p>依Name升冪排序↑<br/>" ); |
048 | lstPerson.Sort( delegate (Personp1, Personp2) { return Comparer< string >.Default.Compare(p1.Name,p2.Name);}); |
049 | foreach (Personp in lstPerson) |
050 | { |
051 | Response.Write(p.ToString()+ "<br/>" ); |
052 | } |
053 |
054 |
055 |
056 | //List<T>.Sort() |
057 | //依Name降冪排序 |
058 | Response.Write( "<p>依Name降冪排序↓<br/>" ); |
059 | lstPerson.Sort( delegate (Personp1, Personp2) { return Comparer< string >.Default.Compare(p2.Name,p1.Name);}); |
060 | foreach (Personp in lstPerson) |
061 | { |
062 | Response.Write(p.ToString()+ "<br/>" ); |
063 | } |
064 | } |
065 | } |
066 |
067 | public class Person |
068 | { |
069 | private int _ID; |
070 | private string _Name; |
071 | private int _Age; |
072 |
073 | public Person( int ID, string Name, int Age) |
074 | { |
075 | _ID= ID; |
076 | _Name= Name; |
077 | _Age= Age; |
078 | } |
079 |
080 | public int ID |
081 | { |
082 | set {_ID= value; } |
083 | get { return _ID;} |
084 | } |
085 |
086 | public string Name |
087 | { |
088 | set {_Name= value; } |
089 | get { return _Name;} |
090 | } |
091 |
092 | public int Age |
093 | { |
094 | set {_Age= value; } |
095 | get { return _Age;} |
096 | } |
097 |
098 | public override string ToString() |
099 | { |
100 | return string .Format( "ID:{0},Name:{1},Age:{2}" , _ID,_Name,_Age); |
101 | } |
102 | } |
相关文章推荐
- [ASP.NET]利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題
- 利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題 转载
- 利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題
- [ASP.NET]利用List的Sort()、Find()、FindAll()、Exist()來解決一些問題
- 利用List的Sort()、Find()、FindAll()、Exist()来解决一些问题
- 利用List的Sort()、Find()、FindAll()、Exist()来解决一些问题
- 利用List的Sort()、Find()、FindAll()、Exist()来解决一些问题
- [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- C#/ASP.NET List中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- 【转载】[C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- 【转】[C#List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#/ASP.NET]List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例
- ASP.NET List<>中Sort()、Find()、FindAll()、Exist()的使用方法
- [C#]List的Sort()、Find()、FindAll()、Exist()的使用方法举例