From 9e350713fe77ee3131a2f25ae0c0d2ca18314110 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=E5=90=B4=E4=BD=B3=E9=A2=96?= <3572749703@qq.com>
Date: Sun, 8 Dec 2024 17:16:44 +0800
Subject: [PATCH] =?UTF-8?q?=E6=93=8D=E4=BD=9C?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
---
...26\350\276\221\346\223\215\344\275\234.md" | 202 +++++++++++
...40\351\231\244\346\237\245\350\257\242.md" | 329 ++++++++++++++++++
2 files changed, 531 insertions(+)
create mode 100644 "\345\220\264\344\275\263\351\242\226/2024.12.2\347\274\226\350\276\221\346\223\215\344\275\234.md"
create mode 100644 "\345\220\264\344\275\263\351\242\226/2024.12.4\345\210\240\351\231\244\346\237\245\350\257\242.md"
diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.2\347\274\226\350\276\221\346\223\215\344\275\234.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.2\347\274\226\350\276\221\346\223\215\344\275\234.md"
new file mode 100644
index 0000000..ac56c15
--- /dev/null
+++ "b/\345\220\264\344\275\263\351\242\226/2024.12.2\347\274\226\350\276\221\346\223\215\344\275\234.md"
@@ -0,0 +1,202 @@
+# 实现 编辑 操作
+
+### 在控制器中
+1. 根据传入的Id,从数据库中拿到**最新**的 值
+```cs
+var blog = Db.Blog.FirstOrDefault(x=>x.Id == input.Id);
+```
+2. 判断是否有对应的对象,有可能找到对应的id记录
+ - 找得到,修改对应的对象,然后保存回数据库,返回列表页
+ ```cs
+ if(blog != null)
+ {
+ blog.Title = input.Title;
+ blog.Content = input.Content;
+ blog.Author = input.Author;
+ }
+ ```
+ - 找不到,直接返回列表页(简单操作)
+
+
+在控制器中,两个Edit对应的代码:
+
+1. 对应Edit的视图
+```cs
+public IActionResult Edit(int id)
+{
+ // 根据传入的要编辑的id,拿到要编辑的博客文章
+ // 通过一定的方式,拿到那个博客
+ var blog = Db.Blog.FirstOrDefault(x=>x.Id == id);
+
+ // 返回给视图
+ return View(blog);
+}
+```
+2. 对应Edit编辑操作页面 post请求
+```cs
+[HttpPost]
+[ValidateAntiForgeryToken] //判断输入的是否为空值,如果是 则提示
+public IActionResult Edit(Blog input)
+{
+ ...
+
+ return View(input);
+}
+```
+
+### 视图中
+内容与添加操作的代码相似
+
+## Linq集成查询
+作用在谁的身上:集合,特别是实现了IEnumerable接口的集合上,方法参数中,一般是Lambda表达式(其实就是匿名函数)
+
+1. 查询单个元素
+ - First() 函数中可以写查找第一个的条件,形如(t => t.Id==id)。但是如果没有第一个元素,或者没有符合条件的第一个元素,则报错
+ - FirstOrDefault() 类似上面的用法,但是在没有符合条件的时候,不报错,而是返回一个null
+ ```cs
+ var blog = Db.Blog.FirstOrDefault(x=>x.Id == id);
+ ```
+
+2. 查询多个元素
+ - Where() 条件函数,可以查找符合一定条件(可多个)的元素,返回的是一个集合
+ ```cs
+ var blog = Db.Blog.Where(x=>x.Id == id && x.Author == "张三");
+ ```
+
+3. 重新返回的数据类型
+ - Select() 这个函数可帮助我们处理函数返回的真正内容
+ ```cs
+ // 可重命名,也可以直接写{ x.Id, x.Author }
+ var blog = Db.Blog.Select(x => new { Xyz = x.Id, Abc = x.Author });
+ ```
+
+## 其他
+- null:没有框框
+- 空值:有框框,但没值(里面没有加入内容)
+
+
+# 作业--综合练习
+## 删除功能
+```html
+@model Blog.Models.Blogs;
+
+
+ | 标题 |
+ @Model.Title |
+
+
+ | 内容 |
+ @Model.Content |
+
+
+ | 作者 |
+ @Model.Author |
+
+
+ |
+ 删除
+ |
+ 取消 |
+
+
+```
+```cs
+ public IActionResult Delete(int id){
+ var blog=Db.Blogs.FirstOrDefault(x=>x.Id==id);
+ if(blog!=null){
+ return View(blog);
+ }
+ return View();
+ }
+ public IActionResult DeleteBtn(int id)
+ {
+ var blog=Db.Blogs.FirstOrDefault(x=>x.Id==id);
+ if(blog!=null){
+ Db.Blogs.Remove(blog);
+ }
+ return RedirectToAction("Index");
+ }
+```
+
+## 新增(或者创建)功能
+```html
+@model Blog.Models.Blogs;
+
+
+```
+```cs
+ public IActionResult Create(){
+ return View();
+ }
+ [HttpPost]
+ public IActionResult Create(Blogs input){
+ if(ModelState.IsValid){
+ var maxId=Db.Blogs.Select(t => t.Id).Max();
+ input.Id=maxId+1;
+ Db.Blogs.Add(input);
+ return RedirectToAction("Index");
+ }
+ return View(input);
+ }
+```
+## 编辑功能
+```html
+@model Blog.Models.Blogs;
+
+
+```
+```cs
+ public IActionResult Edit(int id){
+ var list= Db.Blogs.Where(x => x.Id==id&&x.Author=="呵呵");
+ var list2=Db.Blogs.Select(x => new {Xyz=x.Id,Abc=x.Author});
+ var blog= Db.Blogs.FirstOrDefault(x => x.Id==id);
+ return View(blog);
+ }
+
+ [HttpPost]
+ public IActionResult Edit(Blogs input){
+ if(ModelState.IsValid){
+ var blog=Db.Blogs.FirstOrDefault(x=>x.Id==input.Id);
+ if(blog!=null){
+ blog.Title=input.Title;
+ blog.Author=input.Author;
+ blog.Content=input.Content;
+ }
+ return RedirectToAction("Index");
+ }
+ return View(input);
+ }
+```
+
+## 搜索查询功能
+```cs
+ public IActionResult Index(string keyword){
+ if(string.IsNullOrEmpty(keyword)){
+ return View(Db.Blogs);
+ }
+ else{
+ var list=Db.Blogs.Where(x=>x.Title.Contains(keyword)||x.Author.Contains(keyword)||x.Content.Contains(keyword)).ToList();
+ return View(list);
+
+ }
+ }
+```
+```html
+
+
+
+```
diff --git "a/\345\220\264\344\275\263\351\242\226/2024.12.4\345\210\240\351\231\244\346\237\245\350\257\242.md" "b/\345\220\264\344\275\263\351\242\226/2024.12.4\345\210\240\351\231\244\346\237\245\350\257\242.md"
new file mode 100644
index 0000000..7e03040
--- /dev/null
+++ "b/\345\220\264\344\275\263\351\242\226/2024.12.4\345\210\240\351\231\244\346\237\245\350\257\242.md"
@@ -0,0 +1,329 @@
+## 删除操作
+### 在控制器中的操作
+1. 对应删除页面的试图
+ 1) 根据传入的id,尝试在对应的数据库表中查找对应的记录,找到则返回那个元素,找不到则返回null
+ 2) 如果blog为空,则提示要找到的那个元素不存在,反之 就显示那个元素及信息,准备删除
+ ```cs
+ public IActionResult Delete(int id)
+ {
+ var blog = Db.Blogs.FirstOrDefault(b => b.Id == id);
+ if(blog != null)
+ {
+ return View(blog);
+ }
+
+ return View();
+ }
+ ```
+2. 对应删除操作
+ 1) 根据传入的id,尝试在对应的数据库表中查找对应的记录
+ 2) 删除操作:`Remove()`
+ ```cs
+ public IActionResult DeleteOther(int id)
+ {
+ var blog = Db.Blogs.FirstOrDefault(b => b.Id == id);
+ if(blog != null)
+ {
+ Db.Blogs.Remove(blog);
+ }
+ return RedirectToAction("Index");
+ }
+ ```
+
+### 在视图中
+1. 展现要删除的元素
+2. 确定删除:对应代码元素,一定要获取到对应内容
+ ```html
+ 删除
+ ```
+3. 如果不需要删除,则返回首页
+ ```html
+ 取消删除
+ ```
+
+## 查找操作
+1. 在首页视图中添加查找框(以表单的形式),**在查找框内增加`name = Keyword`属性**,跟查找内容传输相连接
+ ```html
+
+ ```
+2. 在首页的控制器中填入查询代码:
+ - 填写string参数,接收查找框传入的数据
+ - 判断数据是否成功传入
+ - 没有传入,则直接显示首页
+ - 传入了就进行内容匹配,显示匹配结果
+ ```cs
+ public IActionResult Index(string keyword)
+ {
+ if(string.IsNullOrEmpty(keyword))
+ {
+ return View(Db.Blogs);
+ }
+ else
+ {
+ var list = Db.Blogs.Where(x => x.Title.Contains(keyword)
+ || x.Content.Contains(keyword)
+ || x.Author.Contains(keyword))
+ .ToList();
+ return View(list);
+ }
+ }
+ ```
+# 作业
+1. 基础练习
+```cs
+ // 查询特定元素 找出数组中等于5的元素
+ public IActionResult Index(){
+ int[] numbers = {1, 2, 3, 4, 5, 6 };
+ var num=numbers.Where(x=>x==5).ToList();
+ return Ok(num);
+ }
+ // 查询特定范围的元素 找出数组中在2到8之间的元素。
+ public IActionResult num1(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6 };
+ var num=numbers.Where(x=>x>=2&&x<=8).ToList();
+ return Ok(num);
+ }
+ // 查询并转换元素 将数组中的每个数字乘以2。
+ public IActionResult num2(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6 };
+ var num=numbers.Select(x=>x*2).ToList();
+ return Ok(num);
+ }
+
+ // 查询特定属性的对象 找出所有名字以"王"开头的学生。
+ public IActionResult num3(){
+ List students = new List
+ {
+ new Student {Id=1, Name = "王有才", Age = 21 },
+ new Student {Id=2, Name = "王中王", Age = 22 },
+ new Student {Id=3, Name = "张语嫣", Age = 23 },
+ new Student {Id=4, Name = "詹宇航", Age = 35 },
+ new Student {Id=5, Name = "郑雨良", Age = 26 },
+ };
+ var num=students.Where(x=>x.Name.StartsWith('王')).ToList();
+ return Ok(num);
+ }
+
+ // 查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。
+ public IActionResult num4(){
+ List students = new List
+ {
+ new Student {Id=1, Name = "王有才", Age = 21 },
+ new Student {Id=2, Name = "罗婷", Age = 21 },
+ new Student {Id=3, Name = "王中王", Age = 22 },
+ new Student {Id=4, Name = "李子柒", Age = 22 },
+ new Student {Id=5, Name = "张语嫣", Age = 23 },
+ new Student {Id=6, Name = "詹宇航", Age = 35 },
+ new Student {Id=7, Name = "郑雨良", Age = 26 },
+ new Student {Id=8, Name = "欧文", Age = 26 },
+ };
+ var num=students.Where(x=>x.Age>20).OrderByDescending(x=>x.Age).ToList();
+ return Ok(num);
+ }
+ // 查询并去重 找出数组中所有不重复的数字。
+ public IActionResult num5(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Distinct().ToList();
+ return Ok(num);
+ }
+
+ // 查询第一个元素 找出数组中第一个大于3的元素。
+ public IActionResult num6(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.FirstOrDefault(x=>x>3).ToString();
+ return Ok(num);
+ }
+
+
+ // 查询最后一个元素 找出数组中最后一个小于7的元素。
+ public IActionResult num7(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3};
+ var num=numbers.LastOrDefault(x=>x<7).ToString();
+ return Ok(num);
+ }
+
+
+ // 查询元素是否存在 检查数组中是否存在大于10的元素。
+ public IActionResult num8(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ bool num=numbers.Any(x=>x>10);
+ return Ok(num);
+ }
+ // 查询元素的计数 计算数组中大于5的元素数量。
+ public IActionResult num9(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Count(x=>x>5).ToString();
+ return Ok(num);
+ }
+
+ // 查询元素的总和 计算数组中所有元素的总和。
+ public IActionResult num10(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Sum();
+ return Ok(num);
+ }
+ // 查询元素的最大值 找出数组中的最大值。
+ public IActionResult num11(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Max().ToString();
+ return Ok(num);
+ }
+ // 查询元素的最小值 找出数组中的最小值。
+ public IActionResult num12(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Min().ToString();
+ return Ok(num);
+ }
+ // 查询元素的平均值 计算数组中所有元素的平均值。
+ public IActionResult num13(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Average().ToString();
+ return Ok(num);
+ }
+ // 查询特定条件的元素 找出数组中能被3整除的元素。
+ public IActionResult num(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var num=numbers.Where(x=>x%3==0).ToList();
+ return Ok(num);
+ }
+```
+2. 中级练习
+```cs
+ // 查询并选择特定属性 找出所有学生的姓名和年龄。
+ public IActionResult Index(){
+ List students = new List
+ {
+ new Student {Id=1, Name = "王有才", Age = 21 },
+ new Student {Id=2, Name = "罗婷", Age = 21 },
+ new Student {Id=3, Name = "王中王", Age = 22 },
+ new Student {Id=4, Name = "李子柒", Age = 22 },
+ new Student {Id=5, Name = "张语嫣", Age = 23 },
+ new Student {Id=6, Name = "詹宇航", Age = 35 },
+ new Student {Id=7, Name = "郑雨良", Age = 26 },
+ new Student {Id=8, Name = "欧文", Age = 26 },
+ };
+ var result=students.Select(x=>new {x.Name,x.Age}).ToList();
+ return Ok(result);
+ }
+
+ // 查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。
+ public IActionResult Index2(){
+ List students = new List
+ {
+ new Student {Id=1, Name = "王有才", Age = 21 },
+ new Student {Id=2, Name = "罗婷", Age = 21 },
+ new Student {Id=3, Name = "王中王", Age = 22 },
+ new Student {Id=4, Name = "李子柒", Age = 22 },
+ new Student {Id=5, Name = "张语嫣", Age = 23 },
+ new Student {Id=6, Name = "詹宇航", Age = 35 },
+ new Student {Id=7, Name = "郑雨良", Age = 26 },
+ new Student {Id=8, Name = "欧文", Age = 26 },
+ };
+ var result=students.GroupBy(x=>x.Age).Select(t=>new{Age=t.Key,Count=t.Count()}).ToList();
+ return Ok(result);
+ }
+
+ // 查询并联结 联结学生和课程表,找出每个学生的所有课程。
+ public IActionResult Index3(){
+ List students = new List
+ {
+ new Student {Id=1, Name = "王有才", Age = 21 },
+ new Student {Id=2, Name = "罗婷", Age = 21 },
+ new Student {Id=3, Name = "王中王", Age = 22 },
+ new Student {Id=4, Name = "李子柒", Age = 22 },
+ new Student {Id=5, Name = "张语嫣", Age = 23 },
+ new Student {Id=6, Name = "詹宇航", Age = 35 },
+ new Student {Id=7, Name = "郑雨良", Age = 26 },
+ new Student {Id=8, Name = "欧文", Age = 26 },
+ };
+ List courses=new List
+ {
+ new Course{StudentId=1,CourseName="英语"},
+ new Course{StudentId=1,CourseName="数学"},
+ new Course{StudentId=2,CourseName="语文"},
+ new Course{StudentId=3,CourseName="物理"},
+ new Course{StudentId=4,CourseName="化学"},
+ new Course{StudentId=4,CourseName="生物"},
+ new Course{StudentId=4,CourseName="语文"},
+ };
+ var result=students.SelectMany(x=>courses.Where(c=>c.StudentId==x.Id).Select(c=>new{x.Name,c.CourseName})).ToList();
+ return Ok(result);
+ }
+ // 查询并反转 反转数组中的元素顺序。
+ public IActionResult Index4(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var result=numbers.Reverse().ToArray();
+ return Ok(result);
+ }
+ // 查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。
+ public IActionResult Index5(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var result=numbers.Select(x=>x>2?x:numbers.First(x=>x>2)).ToArray();
+ return Ok(result);
+ }
+ // 查询并排除 从数组中排除所有小于5的元素。
+ public IActionResult Index6(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var result=numbers.Where(x=>x>=5).ToArray();
+ return Ok(result);
+ }
+
+ // 查询并填充默认值 如果数组中存在null值,用默认值0替换。
+ public IActionResult Index7(){
+ int?[] nullableNumbers = { 1, null, 3, null, 5 };
+ var result=nullableNumbers.Select(x=>x??0).ToArray();
+ return Ok(result);
+ }
+
+ // 查询并转换类型 将字符串数组转换为整数数组。
+ public IActionResult Index8(){
+ string[] stringNumbers = { "1", "2", "3", "4" };
+ var result=stringNumbers.Select(int.Parse).ToArray();
+ return Ok(result);
+ }
+
+ // 查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。
+ public IActionResult Index9(){
+ object[] objects = { "String", 123, "Another String", 456 };
+ var result = objects.OfType().ToList();
+ return Ok(result);
+ }
+```
+3. 高级练习
+```cs
+ // 查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组
+ public IActionResult Index(){
+ int[] numbers1 = { 1, 2, 3 };
+ int[] numbers2 = { 4, 5, 6 };
+ var result=numbers1.Zip(numbers2,(first,second)=>(first,second)).ToArray();
+ var zipped=numbers1.Zip(numbers2,(first,second)=>new { first, second }).ToArray();
+ return Ok(zipped);
+ }
+ // 查询并使用Range生成 生成一个包含1到10的整数数组。
+ public IActionResult Index2(){
+ int[] result = Enumerable.Range(1,10).ToArray();
+ return Ok(result);
+ }
+ // 查询并使用Repeat重复 重复一个元素多次,创建一个新数组。
+ public IActionResult Index3(){
+ int a = 6;
+ int[] result = Enumerable.Repeat(a,10).ToArray();
+ return Ok(result);
+ }
+ // 查询并使用Take限制数量 从数组中取出前5个元素。
+ public IActionResult Index4(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var result=numbers.Take(5).ToArray();
+ return Ok(result);
+ }
+
+ // 查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。
+ public IActionResult Index5(){
+ int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 };
+ var result=numbers.Skip(3).ToArray();
+ return Ok(result);
+ }
+```
\ No newline at end of file
--
Gitee