.NET基础学习(LINQ)(2)
2、join子句
LINQ中的join子句和SQL中的join很相似,连接操作接受两个集合然后创建一个临时的对象集合,每个对象包含原始集合对象中的所有字段,使用连接来结合两个或更多个集合中的数据。
示例代码如下:
1 public class Student
2 {
3 public int StID;
4 public string LastName;
5 }
6 public class CourseStudent
7 {
8 public string CourseName;
9 public int StID;
10 }
11
12 CourseStudent[] studentInCourses = new CourseStudent[]{
13 new CourseStudent{CourseName="Art",StID=1},
14 new CourseStudent{CourseName="Art",StID=3},
15 new CourseStudent{CourseName="History",StID=1},
16 new CourseStudent{CourseName="History",StID=2},
17 new CourseStudent{CourseName="Physics",StID=3}
18 };
19 Student[] students = new Student[] {
20 new Student{StID=1,LastName="张三"},
21 new Student{StID=2,LastName="李四"},
22 new Student{StID=3,LastName="王五"}
23 };
24
25 //获取选修2门课以上同学的名字
26 var query = (from s in students
27 join c in studentInCourses on s.StID equals c.StID
28 where (from x in studentInCourses where x.StID==c.StID select x.StID).Count()>1
29 select s.LastName).Distinct();
30 foreach (var v in query) Console.Write("{0}\t",v);
31 Console.WriteLine("");
32 //获取选修历史同学的名字
33 var query1 = from s in students
相关新闻>>
- 发表评论
-
- 最新评论 更多>>