diff --git "a/\345\210\230\347\201\277/20241223-\346\216\245\345\217\243&\346\225\260\346\215\256\344\274\252\345\210\240\351\231\244.md" "b/\345\210\230\347\201\277/20241223-\346\216\245\345\217\243&\346\225\260\346\215\256\344\274\252\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..3e87b735905984567cadd6e321d01cd6f2ad55dd --- /dev/null +++ "b/\345\210\230\347\201\277/20241223-\346\216\245\345\217\243&\346\225\260\346\215\256\344\274\252\345\210\240\351\231\244.md" @@ -0,0 +1,26 @@ +## 运行指定文件 +命令:dotnet watch --progect 文件路径 + +## 接口 +在数据集合中,经常会用 lEnumerable、ICollection、lList和 lQueryable 这些类型来定义变量和属性 + +接口之间的关系: +- lList --> ICollection --> lEnumerable <-- lQueryable + +lEnumerable 是所有**非泛型集合**的基本接口 + +## 将数据伪删除 +1. 在类中新增属性:IsDelete(bool类型) +2. 在删除操作的代码中,将删除操作变成更改操作 + ```cs + var obj = _db.Students.FirstOrDefault(x=>x.Id == id); + // 将属性IsDelete的值改为true --> true表示该属性不可用 + obj.IsDelete = true; + _db.Students.Update(obj); + ... + ``` +3. 在Index显示界面的代码中,添加过滤条件 + ```cs + // 表示只有值为false的才显示 + var list = _db.Students.where(x=>x.IsDelete == false).ToList(); + ``` \ No newline at end of file diff --git "a/\345\210\230\347\201\277/20241225-\350\277\236\350\241\250\346\237\245\350\257\242.md" "b/\345\210\230\347\201\277/20241225-\350\277\236\350\241\250\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..e53cabbed0c4cdb1f37fcad71e11825123a1cae8 --- /dev/null +++ "b/\345\210\230\347\201\277/20241225-\350\277\236\350\241\250\346\237\245\350\257\242.md" @@ -0,0 +1,111 @@ +## 操作-连三张表 + +设计数据库: +```js +1. 成绩表 + // + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 学生Id,外键 + /// + public int StudentId { get; set; } + + /// + /// 课程Id,外键 + /// + public int CourseId { get; set; } + + /// + /// 学生的课程成绩 + /// + public decimal Scores { get; set; } + +2. 课程表 + /// + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 课程名称 + /// + public string CourseName { get; set; } = null!; + +3. 学生表 + /// + /// 主键Id + /// + public int Id { get; set; } + + /// + /// 学生编号 + /// + public string StudentCode { get; set; } = null!; + + /// + /// 学生姓名 + /// + public string StudentName { get; set; } = null!; + + /// + /// 昵称 + /// + public string? Nickname { get; set; } + + /// + /// 是否删除,true为已经删除,false为没有删除 + /// + /// + public bool IsDeleted { get; set; } = false; + + public bool IsActived { get; set; } = true; + + public DateOnly Birthday { get; set; } + + public int Age { get; set; } + + public char Sex { get; set; } +``` + + +展示页面:现在有三个表分别为成绩表,课程表,学生信息表,现在要连接三张表形成一张新的表,只显示三张表中的部分内容 +```js + public IActionResult Index() +{ + // 查出所有的成绩记录 + var scores = _db.Scores.ToList(); + // 查出所有的学生记录 + var students = _db.Students.Where(x => !x.IsDeleted).ToList(); + // 查出所有的课程记录 + var courses = _db.Courses.ToList(); + + // 遍历成绩记录 + var res = scores.Select(x => + { + // 找到和当前成绩记录中的学生Id匹配的学生信息 + var tmpStudent = students.FirstOrDefault(z => x.StudentId == z.Id); + // 找到和当前成绩记录中的课程Id匹配的课程信息 + var tmpCourse = courses.FirstOrDefault(z => x.CourseId == z.Id); + + // 分别处理学生、课程没有找到的情况 + var tmpStudentName = tmpStudent == null ? "" : tmpStudent.StudentName; + var tmpCourseName = tmpCourse == null ? "" : tmpCourse.CourseName; + + // 返回新的,包含学生姓名和课程名称的信息 + var res = new + { + x.Id, + x.StudentId, + StudentName = tmpStudentName, + x.CourseId, + CourseName = tmpCourseName, + x.Scores + }; + return res; + }); + return View(res); +} +``` \ No newline at end of file