using System;
using System.Collections.Generic;
using System.Linq;
namespace HelloWorld
{
class Program
{
public class Student
{
public int StID;
public string LastName;
}
public class CourseStudent
{
public string CourseName;
public int StID;
}
static CourseStudent[] studentsInCourses = new CourseStudent[]{
new CourseStudent{CourseName="Art", StID=1},
new CourseStudent{CourseName="Art", StID=2},
new CourseStudent{CourseName="History", StID=1},
new CourseStudent{CourseName="History", StID=3},
new CourseStudent{CourseName="Physics", StID=3},
};
static Student[] students = new Student[]{
new Student{StID=1, LastName="Carson"},
new Student{StID=2, LastName="Klassen"},
new Student{StID=3, LastName="Fleming"},
};
static void Main(string[] args)
{
//查找所有选择了历史课的学生的姓氏
var query = from s in students
join c in studentsInCourses on s.StID equals c.StID
where c.CourseName == "History"
select s.LastName;
//显示所有选择了历史课的学生的名字
foreach(var q in query)
{
Console.WriteLine("Student taking History: {0}", q);
}
}
}
}
C# join子句
最新推荐文章于 2023-04-20 17:44:49 发布