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

Source: Internet
Author: User
Recently, cases are often used. List<T> it's really easy to use.

Because it has the following: List<T>. Sort () → Sort T List<T>. Find () → Find a T List<T> . FindAll() → Find multiple T List<T>. Exist () → judge whether T exists. Let's write an example to introduce these things .. 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 (3, "ASP. NET", 30 ));
LstPerson. Add (new Person (4, "Dotblogs", 40); // the original data is displayed on the GridView.
This. GridView1.DataSource = lstPerson;
This. GridView1.DataBind ();// List<T>. Find ()
// Find the Person whose Name is 'puma'
Response. Write ("find the Person with Name = 'puma' → ");
Response. Write (lstPerson. Find (delegate (Person p) {return p. Name = "puma" ;}). ToString ();// List<T> . FindAll()
// Find the number of Age> 10
Response. Write ("find the number of Age> 10 → ");
Response. Write (lstPerson . FindAll(Delegate (Person p) {return p. Age> 10 ;}). Count. ToString ();// List<T>. Exists ()
// Check whether Name = 'f6 'exists
Response. Write ("check whether Name = 'f6 'exists → ");
Response. Write (lstPerson. Exists (delegate (Person p) {return p. Name = "F6" ;}). ToString ();// List<T>. Sort ()
// Sort by Name power-up
Response. Write ("<p> sort by Name to power up <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 ()
// Sort by Name by power reduction
Response. Write ("<p> sort by Name in descending order <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/>"); ublic 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 );
}
Execution result:

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.