利用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删除。