注:闲来无事 来篇博客
1. 三张表
class Teacher { public int ID { set; get; } public int SchoolNum { set; get; } public string TeacherName { set; get; } public string TeacherAge { set; get; } } class School { public int SchoolNum{ set; get; } public string SchoolName { set; get; } } class Student { public int ID{ set; get; } public int SchoolNum{ set; get; } public string StudentName { set; get; } public string StudentAge { set; get; } }
2.输出 School 表中对应人员 Teacher 和 Student 的信息,包括 城市姓名、人员姓名、人员年龄
3.代码
var schoolModel = db.school.ToList(); var teacherModel = db.teacher.ToList(); var result1 = schoolModel.Join(teacherModel, p => p.CityNum, c => c.CityNum, (p, c) => new { CityName = p.CityName, PeopleName = c.TeacherName, PeopleAge = c.TeacherAge }).ToList(); var studentModel = db.student.ToList(); var result2 = schoolModel.Join(studentModel, p => p.CityNum, c => c.CityNum, (p, c) => new { CityName = p.CityName, PeopleName = c.StudentName, PeopleAge = c.StudentAge }).ToList(); result1.AddRange(result2);
4.输出
{ CityName = A学校, PeopleName = "学生A", PeopleAge = "学生A" }
{ CityName = A学校, PeopleName = "教师A", PeopleAge = "教师A" }
{ CityName = B学校, PeopleName = "学生B", PeopleAge = "学生B" }
{ CityName = C学校, PeopleName = "学生C", PeopleAge = "学生C" }