C#中LINQ的使用demo

 1  public void showTolist(int type)
 2         {
 3             listbox.Items.Clear();
 4             listbox.Items.Add("姓名     年龄      手机号码");
 5             if (type == 1)
 6             {   
 7                 foreach (var n in list)
 8                 {
 9                     listbox.Items.Add(n.print());
10                 }
11             }else
12                 if (type == 2)
13                 {
14                     var query1 = from t1 in list
15                                  where t1.getAge() >= 18
16                                  select new { name=t1.getName(),age=t1.getAge(),
17                                               telephone=t1.getTelephone()}; 
18                     var query2 = from t2 in query1
19                                  where t2.age <= 20
20                                  select  new {name=t2.name,age=t2.age,
21                                               telephone=t2.telephone};
22                     foreach (var n in query2)
23                     {
24                         listbox.Items.Add(n.name+"   "+n.age+"      "+n.telephone );
25                     }
26 
27                 }
28                 else
29                 {
30                     var qu = from t in list
31                              where t.getName().ElementAt(0).ToString() == ""
32                              select new {name=t.getName(),age=t.getAge(),
33                                               telephone=t.getTelephone()};
34                     foreach (var n in qu)
35                     {
36                         listbox.Items.Add(n.name + "   " + n.age + "      " + n.telephone);
37                     }
38 
39                 }

 

posted on 2016-05-10 23:23  一梦千寻  阅读(798)  评论(0编辑  收藏  举报

导航