Use sort (), find (), findall (), and exist () of the list to solve some problems.

Source: Internet
Author: User

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" %>
02  
03 <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
04  
05 <Html Xmlns=Http://www.w3.org/1999/xhtml">
06 <Head Runat="Server">
07 <Title> Genericlist </Title>
08 </Head>
09 <Body>
10 <Form ID="Form1" Runat="Server">
11 <Div>
12 Raw materials:
13 <ASP: gridview ID="Gridview1" Runat="Server">
14 </ASP: gridview>
15 </Div>
16 </Form>
17 </Body>
18 </Html>

Genericlist. aspx. CS

View Source

Print?

001 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 // Raw information is displayed on the gridview
019 This. Gridview1.datasource = lstperson;
020 This. Gridview1.databind ();
021  
022  
023  
024 // List <t>. Find ()
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>");
028  
029  
030  
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>");
035  
036  
037  
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>");
042  
043  
044  
045 // List <t>. Sort ()
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 PIn Lstperson)
050 {
051 Response. Write (P. tostring () +"<Br/>");
052 }
053  
054  
055  
056 // List <t>. Sort ()
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 PIn 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 }

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.