EXAMPLE FOR LINQ TO OBJECT

10983 ワード


  
  
  
  
  1. using System; 
  2. using System.Collections.Generic; 
  3. using System.Linq; 
  4. using System.Text; 
  5.  
  6. namespace LINQ 
  7.     class student 
  8.     { 
  9.         private string name; 
  10.  
  11.         public string MyName 
  12.         { 
  13.             get { return name; } 
  14.             set { name = value; } 
  15.         } 
  16.  
  17.         private string sex; 
  18.  
  19.         public string MySex 
  20.         { 
  21.             get { return sex; } 
  22.             set { sex = value; } 
  23.         } 
  24.         private int age; 
  25.  
  26.         public int MyAge 
  27.         { 
  28.             get { return age; } 
  29.             set { age = value; } 
  30.         } 
  31.  
  32.         private string schoolname; 
  33.  
  34.         public string MySchoolName 
  35.         { 
  36.             get { return schoolname; } 
  37.             set { schoolname = value; } 
  38.         } 
  39.  
  40.         private string classname; 
  41.  
  42.         public string MyClassName 
  43.         { 
  44.             get { return classname; } 
  45.             set { classname = value; } 
  46.         } 
  47.  
  48.  
  49.     } 
  50.     class schoolinfo 
  51.     { 
  52.         private string schoolname; 
  53.  
  54.         public string MySchoolName 
  55.         { 
  56.             get { return schoolname; } 
  57.             set { schoolname = value; } 
  58.         } 
  59.  
  60.         private string classname; 
  61.  
  62.         public string MyClassName 
  63.         { 
  64.             get { return classname; } 
  65.             set { schoolname = value; } 
  66.         } 
  67.  
  68.  
  69.     } 
  70.     class Program 
  71.     { 
  72.         public delegate void ShowSudentMessage(); 
  73.         static void Main(string[] args) 
  74.         {    
  75.              
  76.             List<student> students = new List<student>() 
  77.             { 
  78.                 new student{MyName = "zhangsan",MyAge = 21, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"}, 
  79.                 new student{MyName = "lixi",MyAge = 22, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"}, 
  80.                 new student{MyName = "wangwu",MyAge = 23, MyClassName = "CS-03",MySchoolName = "NCUT",MySex = "MALE"
  81.             }; 
  82.             var schoolinfo = from p in students 
  83.                              where p.MyAge < 30 
  84.                              select new 
  85.                              { 
  86.                                  SchoolName = p.MySchoolName, 
  87.                                  ClassName = p.MyClassName 
  88.                              }; 
  89.  
  90.             ShowMessage(() => 
  91.             { 
  92.                 foreach (var item in schoolinfo) 
  93.                 { 
  94.                     Console.WriteLine(item.SchoolName + "  " + item.ClassName); 
  95.                 } 
  96.             }); 
  97.             Console.ReadKey(); 
  98.         } 
  99.         static void ShowMessage(ShowSudentMessage ExecuteShow) 
  100.         { 
  101.             ExecuteShow(); 
  102.         } 
  103.     }