Query the data in the list using LINQ

Source: Internet
Author: User
View code

 1   //  1. query all the girls in the list, whose ages are less than 18, and sort them in descending order.  2   //  2. query the students whose names start with "Wang" and whose length is 3.  3   Using  System;  4   Using  System. Collections. Generic;  5   Using  System. LINQ; 6   Public   Class  Studylinq  7   {  8       Public   Static   Void  Main ()  9   {  10 List <people> List = New List <people> (); 11 List. Add ( New People () {name = "  Kangxi  " , Age = 18 , Sex = "  Male  "  });  12 List. Add ( New People () {name = "  Yongzheng  " , Age =20 , Sex = "  Male  "  });  13 List. Add ( New People () {name = "  Qianlong District  " , Age = 18 , Sex = "  Male  "  });  14 List. Add ( New People () {name = "  Wang Xiaozhi  " , Age = 23 , Sex = "  Male  "  });  15 List. Add ( New People () {name = "  Wu Zetian  " , Age = 17 , Sex = "  Female  "  });  16 List. Add ( New People () {name = "  Cixi  " , Age = 16 , Sex = "  Female  "  });  17           VaR Result = List. Where (P => P. age < 18 & P. Sex = "  Female  " ). Orderbydescending (P => P. Age );  18               Foreach (PEOPLE In  Result)  19   {  20 Console. writeline (A. Name + "    " +A. Age );  21   }  22           VaR Result1 = List. Where (P => P. Name. startswith ( "  Wang  " ) & P. Name. Length = 3  );  23               Foreach (PEOPLE B In  Result1)  24  {  25   Console. writeline (B. Name );  26   }  27   }  28       Public   Class  People  29   {  30           Public   String  Name 31   {  32               Get ; Set  ;  33   }  34           Public   Int  Age  35   {  36               Get ; Set ;  37   }  38           Public   String  Sex  39   {  40               Get ; Set  ;  41   }  42   } 43 }

 

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.