From 28081c3c86ff0530d86a0bd5fbd1144a3100d02a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=81=BF?= <2262917336@qq.com> Date: Tue, 24 Dec 2024 21:42:53 +0800 Subject: [PATCH 1/2] =?UTF-8?q?20241223=EF=BC=9A=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E5=92=8C=E6=95=B0=E6=8D=AE=E4=BC=AA=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\344\274\252\345\210\240\351\231\244.md" | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 "\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" 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 0000000..3e87b73 --- /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 -- Gitee From da03d6aa02d116b32911388e38488df1b147e9ca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E7=81=BF?= <2262917336@qq.com> Date: Sun, 29 Dec 2024 20:50:33 +0800 Subject: [PATCH 2/2] =?UTF-8?q?20241225=EF=BC=9A=E8=BF=9E3=E5=BC=A0?= =?UTF-8?q?=E8=A1=A8=E6=9F=A5=E8=AF=A2=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...36\350\241\250\346\237\245\350\257\242.md" | 111 ++++++++++++++++++ 1 file changed, 111 insertions(+) create mode 100644 "\345\210\230\347\201\277/20241225-\350\277\236\350\241\250\346\237\245\350\257\242.md" 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 0000000..e53cabb --- /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 -- Gitee