1、查询出list中所有女生并且年龄小于18,并按降序排列

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 public class Linq
 5 {
 6     public static void Main()
 7     {
 8         List<People> list = new List<People>();
 9         list.Add(new People(){Name="王艳",Age=10,Sex=""});
10         list.Add(new People(){Name="王晓华",Age=12,Sex=""});
11         list.Add(new People(){Name="张三",Age=15,Sex=""});
12         list.Add(new People(){Name="李四",Age=18,Sex=""});
13         list.Add(new People(){Name="赵云",Age=21,Sex=""});
14         list.Add(new People(){Name="何艳",Age=22,Sex=""});
15         list.Add(new People(){Name="周华",Age=20,Sex=""});
16 
17         //查询出list中所有女生并且年龄小于18,并按降序排列
18         var result = list.Where(p=>p.Age<18 && p.Sex=="").OrderByDescending(p=>p.Age);
19         foreach(People pp in result)
20         {
21             Console.WriteLine(pp.Name+"  "+pp.Age+"  "+pp.Sex);
22         }
23             }
24 }
25 public class People
26 {
27     public string Name
28     {get;set;}
29     public int Age
30     {get;set;}

 

2、查询出list中名字为“王”开头,并且长度为3的学生

View Code
 1 using System;
 2 using System.Collections.Generic;
 3 using System.Linq;
 4 public class Linq
 5 {
 6     public static void Main()
 7     {
 8         List<People> list = new List<People>();
 9         list.Add(new People(){Name="王艳",Age=10,Sex=""});
10         list.Add(new People(){Name="王晓华",Age=12,Sex=""});
11         list.Add(new People(){Name="张三",Age=15,Sex=""});
12         list.Add(new People(){Name="李四",Age=18,Sex=""});
13         list.Add(new People(){Name="赵云",Age=21,Sex=""});
14         list.Add(new People(){Name="何艳",Age=22,Sex=""});
15         list.Add(new People(){Name="周华",Age=20,Sex=""});
16         //查询出list中名字为“王”开头,并且长度为3的学生
17         var result1 = from p in list
18                       where p.Name.Length==3 &&
19                        p.Name.StartsWith("")
20                       select p;
21         foreach(People pp in result1)
22         {
23             Console.WriteLine(pp.Name+"  "+pp.Age+"  "+pp.Sex);
24         }
25     }
26 }
27 public class People
28 {
29     public string Name
30     {get;set;}
31     public int Age
32     {get;set;}

 

posted on 2012-08-01 19:49  午后の時間  阅读(230)  评论(0编辑  收藏  举报