Use Sort (), Find (), FindAll (), and Exist () of the List to solve some problems.

Source: Internet
Author: User
Use Sort (), Find (), FindAll (), and Exist () of the List to solve some problems.

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 p in 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 p 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 }

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.