List查询语句

要查询的对象 



 1 public partial class XTeacher
 2     {
 3         private string m_Name = string.Empty;
 4 
 5         public string Name
 6         {
 7             get { return this.m_Name; }
 8             set { this.m_Name = value; }
 9         }
10 
11         private string m_Id = string.Empty;
12 
13         public string Id
14         {
15             get { return this.m_Id; }
16             set { this.m_Id = value; }
17         }
18 
19         public XTeacher(string name, string id)
20         {
21             this.m_Name = name;
22             this.m_Id = id;
23         }
24     }

 定义查询类

1  public partial class XTeacherFinder
 2     {
 3         private string m_TeacherId = string.Empty;
 4 
 5         public string TeacherId
 6         {
 7             get { return m_TeacherId; }
 8             set { this.m_TeacherId = value; }
 9         }
10 
11         private string m_TeacherName = string.Empty;
12 
13         public string TeacherName
14         {
15             get { return this.m_TeacherName; }
16             set { this.m_TeacherName = value; }
17         }
18 
19         public XTeacherFinder()
20         {
21 
22         }
23 
24         /// 
25         /// 通过ID查找
26         /// 
27         /// 
28         /// 
29         public bool FindTeacherById(XTeacher teacher)
30         {
31             return m_TeacherId == teacher.Id;
32         }
33 
34         /// 
35         /// 通过名称查找
36         /// 
37         /// 
38         /// 
39         public bool FindTeacherByName(XTeacher teacher)
40         {
41             return m_TeacherName == teacher.Name;
42         }
43 
44         /// 
45         /// 通过ID和名称查找
46         /// 
47         /// 
48         /// 
49         public bool FindTeacherByIdAndName(XTeacher teacher)
50         {
51             return (m_TeacherName == teacher.Name && m_TeacherId == teacher.Id);
52         }
53     }

客户端调用查询 

复制代码

 1   static void Main(string[] args)
 2         {
 3 
 4             List teachers = new List();
 5 
 6             XTeacher teacher = new XTeacher("test1", "1");
 7             teachers.Add(teacher);
 8 
 9             teacher = new XTeacher("test2", "2");
10             teachers.Add(teacher);
11 
12             teacher = new XTeacher("test2", "3");
13             teachers.Add(teacher);
14 
15             XTeacherFinder teacherFinder = new XTeacherFinder();
16             teacherFinder.TeacherId = "1";
17 
18             XTeacher findedTeacher = teachers.Find(teacherFinder.FindTeacherById);
19             Console.WriteLine("通过ID找到教师:{0},{1}", findedTeacher.Id, findedTeacher.Name);
20 
21             XTeacherFinder finder=new XTeacherFinder();
22             finder.TeacherName="test2";
23             XTeacher findedTeacherByName = teachers.Find(finder.FindTeacherByName);
24             Console.WriteLine("通过名称找到教师:{0},{1}", findedTeacherByName.Id, findedTeacherByName.Name);
25 
26             List findedTeachers = teachers.FindAll(finder.FindTeacherByName);
27             foreach (XTeacher t in findedTeachers)
28             {
29                 Console.WriteLine("通过名称找到教师集合:{0},{1}", t.Id, t.Name);
30             }
31 
32             XTeacherFinder f = new XTeacherFinder();
33             f.TeacherId = "3";
34             f.TeacherName = "test2";
35             XTeacher fTeacher = teachers.Find(f.FindTeacherByIdAndName);
36             Console.WriteLine("通过ID和名称找到教师:{0},{1}", fTeacher.Id, fTeacher.Name);
37 
38             Console.Read();
39         }

 

 

你可能感兴趣的:(程序)