lambda一些查询语句
<!--得分数据结构-->
1 <Score> 2 <studentid>1</studentid> 3 <courseid>1</courseid> 4 <score>80</score> 5 </Score>
<!--科目数据结构-->
1 <Course> 2 <courseid>1</courseid> 3 <name>攻击力</name> 4 </Course>
<!--学生数据结构-->
1 <Student> 2 <studentid>1</studentid> 3 <name>大娃</name> 4 <sex>男</sex> 5 <age>13</age> 6 <remark>力大无穷</remark> 7 </Student>
1 //按年龄排序 2 public static List<Student> GetStudentsSortByAge() 3 { 4 try 5 { 6 //return Global.Data_students.OrderBy(stu => stu.age).ToList(); 7 return Global.Data_students.OrderByDescending(stu => stu.age).ToList(); 8 } 9 catch (Exception exp) 10 { } 11 return null; 12 }
1 //指定年龄区间 2 public static List<Student> GetStudentsByAgeBetween(int sage,int bage) 3 { 4 try 5 { 6 return Global.Data_students.Where(stu => (stu.age >= sage && stu.age < bage)).ToList(); 7 } 8 catch (Exception exp) 9 { } 10 return null; 11 }
//姓名模糊查询 public static List<Student> GetStudentsByNameContains(string name) { try { return Global.Data_students.Where(stu =>stu.name.Contains(name)).ToList(); } catch (Exception exp) { } return null; }
//指定性别查询 public static List<Student> GetStudentsBySexIs(string sex) { try { return Global.Data_students.Where(stu => stu.sex==sex).ToList(); } catch (Exception exp) { } return null; }
1 //多表查询 2 public static List<Result> GetScores() 3 { 4 try 5 { 6 List<Result> lr = new List<WindowsFormsApplication1.Result>(); 7 foreach (var v in Global.Data_scores.Join(Global.Data_courses, score => score.courseid, course => course.courseid, (score, course) => new 8 { 9 studentid = score.studentid, 10 coursename = course.name, 11 scores = score.score 12 }).Join(Global.Data_students, score => score.studentid, student => student.studentid, (score, student) => new 13 { 14 studentname = student.name, 15 coursename = score.coursename, 16 scores = score.scores 17 }).ToList() 18 ) 19 { 20 Result r = new Result(); 21 r.coursename = v.coursename; 22 r.score = v.scores; 23 r.studentname = v.studentname; 24 lr.Add(r); 25 }; 26 return lr; 27 } 28 catch (Exception exp) 29 { } 30 return null; 31 }