利用Linq查詢List中的數據


View Code
 1 //1,查詢出list中所有女生並且年齡小於18,並按降序排列
 2 //2,查詢出list中名字為“王”開頭,並且長度為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 = "康熙",Age = 18 ,Sex = ""});
12         list.Add(new People(){Name = "雍正",Age = 20 ,Sex = ""});
13         list.Add(new People(){Name = "乾隆",Age = 18 ,Sex = ""});
14         list.Add(new People(){Name = "王羲之",Age = 23 ,Sex = ""});
15         list.Add(new People(){Name = "武則天",Age = 17 ,Sex = ""});
16         list.Add(new People(){Name = "慈禧",Age = 16 ,Sex = ""});
17         var result = list.Where (p=>p.Age<18 && p.Sex =="").OrderByDescending(p=>p.Age);
18             foreach (People a in result)
19             {
20                 Console.WriteLine(a.Name + "  "+a.Age);
21             }
22         var result1 = list.Where (p=>p.Name.StartsWith ("") && 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 }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM