Original article address:
Http://www.dotblogs.com.tw/puma/archive/2009/05/28/asp.net-generic-list-sort-find-findall-exsit.aspx#12190
List is often used in recent cases. This is really useful.
Because it has the following things:
List <t>. Sort () → sort t
List <t>. Find () → find a t
List <t>. findall () → find multiple T
List <t>. exist () → judge whether the existence of limit t exists
Let's talk about these things ..
Genericlist. aspx
View Source
Print?
01 |
<% @ Page Language = "C #" autoeventwireup = "true" codefile = "genericlist. aspx. cs" inherits = "genericlist" %> |
03 |
<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
05 |
< Html Xmlns = Http://www.w3.org/1999/xhtml" > |
07 |
< Title > Genericlist </ Title > |
10 |
< Form ID = "Form1" Runat = "Server" > |
13 |
< ASP: gridview ID = "Gridview1" Runat = "Server" > |
Genericlist. aspx. CS
View Source
Print?
002 |
Using System. Collections. Generic; |
004 |
Using System. Web. UI; |
005 |
Using System. Web. UI. webcontrols; |
007 |
Public Partial Class Genericlist: system. Web. UI. Page |
010 |
Protected Void Page_load ( Object Sender, eventargs E) |
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 )); |
018 |
// Raw information is displayed on the gridview |
019 |
This . Gridview1.datasource = lstperson; |
020 |
This . Gridview1.databind (); |
025 |
// Find the person whose name is 'puma' |
026 |
Response. Write ( "Find the person with name = 'puma '→" ); |
027 |
Response. Write (lstperson. Find ( Delegate (Person p ){ Return P. Name = "Puma" ;}). Tostring () + "<P>" ); |
031 |
// List <t>. findall () |
032 |
// Locate the number of age> 10 |
033 |
Response. Write ( "Find the number of age> 10 →" ); |
034 |
Response. Write (lstperson. findall ( Delegate (Person p ){ Return P. age> 10;}). Count. tostring () + "<P>" ); |
038 |
// List <t>. exists () |
039 |
// Check whether name = 'f6 'exists |
040 |
Response. Write ( "Checking whether name = 'f6 'exists →" ); |
041 |
Response. Write (lstperson. exists ( Delegate (Person p ){ Return P. Name = "F6" ;}). Tostring () + "<P>" ); |
046 |
// Sort by name in ascending order |
047 |
Response. Write ( "<P> sort by name in ascending order <br/>" ); |
048 |
Lstperson. Sort ( Delegate (Person P1, person P2 ){ Return Comparer < String >. Default. Compare (p1.name, p2.name );}); |
049 |
Foreach (Person P In Lstperson) |
051 |
Response. Write (P. tostring () + "<Br/>" ); |
057 |
// Sort by name in descending order |
058 |
Response. Write ( "<P> sort by name in descending order <br/>" ); |
059 |
Lstperson. Sort ( Delegate (Person P1, person P2 ){ Return Comparer < String >. Default. Compare (p2.name, p1.name );}); |
060 |
Foreach (Person P In Lstperson) |
062 |
Response. Write (P. tostring () + "<Br/>" ); |
070 |
Private String _ Name; |
073 |
Public Person ( Int ID, String Name, Int Age) |
088 |
Set {_ Name = value ;} |
089 |
Get { Return _ Name ;} |
094 |
Set {_ Age = value ;} |
098 |
Public Override String Tostring () |
100 |
Return String . Format ( "ID: {0}, name: {1}, age: {2 }" , _ Id, _ name, _ age ); |
The result of the rows:
Exam Website:
Http://blogs.msdn.com/kcwalina/archive/2004/06/22/162533.aspx
Http://www.dotblogs.com.tw/chhuang/archive/2008/05/08/3908.aspx