From 187f7ca85cd381d2846491a939f1bbf8ce5b2eb9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E4=BD=99=E5=80=A9?= <819139773@qq.com> Date: Sun, 8 Dec 2024 17:18:30 +0800 Subject: [PATCH] =?UTF-8?q?20241202-04=E7=BC=96=E8=BE=91=E5=88=A0=E9=99=A4?= =?UTF-8?q?=E6=9F=A5=E6=89=BE?= 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" | 68 +++ ...45\346\211\276\346\223\215\344\275\234.md" | 519 ++++++++++++++++++ 2 files changed, 587 insertions(+) create mode 100644 "\346\236\227\344\275\231\345\200\251/20241202\347\274\226\350\276\221\346\223\215\344\275\234.md" create mode 100644 "\346\236\227\344\275\231\345\200\251/20241204\345\210\240\351\231\244\344\270\216\346\237\245\346\211\276\346\223\215\344\275\234.md" diff --git "a/\346\236\227\344\275\231\345\200\251/20241202\347\274\226\350\276\221\346\223\215\344\275\234.md" "b/\346\236\227\344\275\231\345\200\251/20241202\347\274\226\350\276\221\346\223\215\344\275\234.md" new file mode 100644 index 0000000..70117d0 --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241202\347\274\226\350\276\221\346\223\215\344\275\234.md" @@ -0,0 +1,68 @@ +## 一.笔记 +### 1. Linq 集成查询 +```js +作用在谁身上:集合,特别是实现了IEnumerable接口的集合上,方法参数中,一般是Lambda表达式(其实就是匿名函数) + + - 查询单个元素 + - .Frist() 函数中可以写查找第一个条件,形如(t=>t.Id==id)。但是,如果没有第一个元素,或者没有符合条件第一个元素,则报错 + - .FristOrDefault() 类似上面的用法,但是在没有符合条件的时候,不报错,而是返回一个null + - 查询多个元素 + - Where() 条件函数,可以查找符合一定条件的元素,返回的是一个集合 + + - 重新设计返回的数据类型 + - select() 这个函数可以帮助我们处理函数返回的真正内容 + + [ValidateAntiForgeryToken] + 是一种用于防止跨站请求伪造(CSRF)攻击的机制。在ASP.NET MVC中,它用于确保提交到服务器的表单数据是由同一用户会话中的合法客户端生成的,而不是由恶意网站生成的。 + + ‌ModelState.IsValid‌ + 是ASP.NET MVC 和 ASP.NET Core 中用于表示模型数据是否有效的布尔属性。当模型的数据验证通过时,ModelState.IsValid 的值为 true;如果数据验证失败,则值为 false。 + if (ModelState.IsValid) { + // 数据有效,执行相应的操作 + } +``` + + +## 二.作业-编辑页面操作 + +控制器 +```js +// 编辑 + [HttpPost] + public IActionResult Edit(Shuju input) + { + // 根据数据传入的id,从数据库中拿到最新值 + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==input.Id); //用户输入的id和内容中的id一致 根据id拿到最新值 + + // 判断是否有对应的对象 + // 找到后,修改值,保存到数据库中,返回首页 + if(cont!=null){ + cont.Title=input.Title; + cont.Content=input.Content; + cont.Author=input.Author; + } + + //找不到,简单返回首页 + + return RedirectToAction("index");//添加完成后返回主页面 + } + public IActionResult Edit(int Id) + { + // 根据传入的id,编辑要修改的内容 将内容传递到编辑框中 + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==Id); + // 传递的内容返回到视图 + return View(cont); + } +``` +视图 +```js +@model New.Models.Shuju + +
+
+
+
+
+ +
+``` \ No newline at end of file diff --git "a/\346\236\227\344\275\231\345\200\251/20241204\345\210\240\351\231\244\344\270\216\346\237\245\346\211\276\346\223\215\344\275\234.md" "b/\346\236\227\344\275\231\345\200\251/20241204\345\210\240\351\231\244\344\270\216\346\237\245\346\211\276\346\223\215\344\275\234.md" new file mode 100644 index 0000000..549ae95 --- /dev/null +++ "b/\346\236\227\344\275\231\345\200\251/20241204\345\210\240\351\231\244\344\270\216\346\237\245\346\211\276\346\223\215\344\275\234.md" @@ -0,0 +1,519 @@ +## 一.笔记 +### 1.删除操作 +1. 方法一:使用post表单形式 +```js + +**在控制器中**: + // 控制的是删除的页面 + public IActionResult Delete(int id) + { + // 将内容显示到视图(这是一个删除的页面 显示删除还是不删的页面) + + // 根据提供(传入的)id,尝试在对应的数据库表中查找对应的记录,找到则返回那个元素,找不到则返回null + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + + // 如果cont为null,则提示要找的那个元素不存在,反之,就显示那个元素及信息,准备删除 + + if (cont!=null) + { + return View(cont); + }; + return View(); + } + + // 控制的是删除的操作 + [HttpPost] + public IActionResult DeleteCont(int id) + { + // 删除方法1:post 使用表单形式 + // 这个将页面内容删除 使用post表单删除的方法 + + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + // 如果不为空则删除 + if (cont!=null) + { + Db.Shuju.Remove(cont); + }; + // 删除后返回首页 + return RedirectToAction("Index"); + } + + + +**在视图中**: +
+
+ +
+ + +``` +2. 方法二:get a标签 +```js + + +**在控制器中**: + // 控制的是删除的页面 + public IActionResult Delete(int id) + { + // 将内容显示到视图(这是一个删除的页面 显示删除还是不删的页面) + + // 根据提供(传入的)id,尝试在对应的数据库表中查找对应的记录,找到则返回那个元素,找不到则返回null + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + + // 如果cont为null,则提示要找的那个元素不存在,反之,就显示那个元素及信息,准备删除 + + if (cont!=null) + { + return View(cont); + }; + return View(); + } + // 控制的是删除的操作 + public IActionResult DeleteOther(int id) + { + // 删除方法2:get 不使用表单形式,使用a标签对应的id进行删除 + + var cont = Db.Shuju.FirstOrDefault(x=>x.Id==id); + // 如果不为空则删除 + if (cont!=null) + { + Db.Shuju.Remove(cont); + }; + // 删除后返回首页 + return RedirectToAction("Index"); + } + + +**在视图中**: + // 使用a标签 根据id来操作 + 删除 + +``` + + +### 2. 查询操作 +1. 在控制器中 +```js + // 首页 + // 传入要处理的变量名 由于用户输入的可能是字符串使用string 处理在index页面中名为keyword的数据 + public IActionResult Index(string keyword) + { + // 判断字符串是否为空 如果用户输入的查询内容为空 那么就显示所有的数据 + if(string.IsNullOrEmpty(keyword)) + { + // 显示所有的数据 + return View(Db.Shuju); + } + // 如果用户输入的是能查询到的 那么就返回查询结果 反之就显示为空 + else + { + // Contains 判断集合中的数据是否有包含用户输入的内容 + var list = Db.Shuju.Where(x=>x.Title.Contains(keyword) + ||x.Content.Contains(keyword) + ||x.Author.Contains(keyword)).ToList(); + // 有则返回查询的结果 + return View(list); + } + } + +``` +2. 在视图中 +```js +
+ {/* get请求 */} +
+ + +
+
+``` + + + + +## 二.Linq集成查询和Lambda表达式 +### 1. 基础 +```js + public IActionResult One() + { + // 1.查询特定元素 找出数组中等于5的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var number1 = numbers.Where(n=>n==5).ToList(); + + return View(number1); + } + public IActionResult Two() + { + // 2.查询特定范围的元素 找出数组中在2到8之间的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num2 = numbers.Where(n => n>=2&&n<=8 ); + + return View(num2); + } + public IActionResult Three() + { + // 3.查询特定范围的元素 找出数组中在2到8之间的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num2 = numbers.Select(n => n*2 ); + + return View(num2); + } + public IActionResult Four() + { + // 4.查询特定属性的对象 找出所有名字以"王"开头的学生。 + + 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 }, + }; + // StartsWith是字符串的一个方法,用于检查字符串是否以指定的前缀开始 + var wang = students.Where(n=>n.Name.StartsWith("王")); + + return View(wang); + } + public IActionResult Five () + { + // 5.查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + + 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 }, + }; + // OrderByDescending降序,OrderBy升序 + var big = students.Where(n=>n.Age>20).OrderByDescending(n=>n.Age); + return View(big); + } + public IActionResult Six () + { + // 6.查询并去重 找出数组中所有不重复的数字。 + + // 1.去重,有两次相同的数字值显示一个 + // int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // var num= numbers.Distinct().ToArray(); + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + + // 2.去重。并且不显示重复的数字 Dictionary + // 利用GroupBy和ToDictionary方法统计了每个数字的出现次数 + var shu = numbers.GroupBy(n=>n).ToDictionary(n=>n.Key,n=>n.Count()); + // 筛选出不出现两次的数字 + var shu2 = numbers.Where(n=>shu[n]!=2); + + + return View(shu2); + } + public IActionResult Seven () + { + // 7.查询第一个元素 找出数组中第一个大于3的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var os = numbers.Where(n=>n>3).First(); + return View(os); + } + public IActionResult Eight () + { + //8.查询最后一个元素 找出数组中最后一个小于7的元素。 + // LastOrDefault 查找最后一个 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.LastOrDefault(n=>n<7); + + return View(num); + } + public IActionResult Nine () + { + //9.查询元素是否存在 检查数组中是否存在大于10的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + bool num = Array.Exists(numbers,num=>num>10); + + ViewBag.nums=num; + + return View(); + } + public IActionResult Ten () + { + //10.查询元素的计数 计算数组中大于5的元素数量。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Count(n=>n>5); + + return View(num); + + } + public IActionResult Eleven () + { + //11 查询元素的总和 计算数组中所有元素的总和。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var all = numbers.Sum(n=>n); + return View(all); + + } + public IActionResult Twelve () + { + //12 查询元素的最大值 找出数组中的最大值。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var max = numbers.Max(n=>n); + return View(max); + } + public IActionResult Thirteen () + { + //13 查询元素的最小值 找出数组中的最小值。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var minnum = numbers.Min(n=>n); + return View(minnum); + } + public IActionResult Fourteen () + { + //14 查询元素的平均值 计算数组中所有元素的平均值。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var avg = numbers.Average(n=>n); + return View(avg); + } + public IActionResult Fifteen () + { + //15 查询特定条件的元素 找出数组中能被3整除的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Where(n=>n%3==0); + return View(num); + } + public class Student +{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} +public class Course +{ + public int StudentId{get;set;} + public string CourseName{get;set;}=null!; + +} +``` +### 2. 中级 +```js + public IActionResult Sixteen () + { + //16 查询并选择特定属性 找出所有学生的姓名和年龄。 + + 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 stu = students.Select(n=>new{n.Name,n.Age}).ToList(); + + return View(stu); + } + public IActionResult Seventeen () + { + //17 按年龄分组学生,并计算每个年龄组的学生数量。 + + 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 stu = students.GroupBy(n=>n.Age).Select(n=>new{Age=n.Key,Count=n.Count()}); + return View(stu); + } + + + public IActionResult Eighteen() + { + //18 查询并联结 联结学生和课程表,找出每个学生的所有课程。 + 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 stu = students.Select(students=>new{ + StudentId = students.Id, + StudentName = students.Name, + Courses = courses + .Where(course => course.StudentId == students.Id) + .Select(course => course.CourseName) + .ToList() + }).ToList(); + + + return View(stu); + } + public IActionResult Nineteen() + { + //19 查询并反转 反转数组中的元素顺序。 + // Reverse() + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Reverse(); + return View(num); + } + public IActionResult Twenty() + { + // 20 查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 查询大于2的,并且使大于2的数都等于2 + var shu = numbers.Where(n=>n>2).Select(n=>2); + + return View(shu); + } + public IActionResult Twenty_One() + { + // 21 查询并排除 从数组中排除所有小于5的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 那么就是 只取大于5的元素 + var num = numbers.Where(n=>n>5); + + return View(num); + } + public IActionResult Twenty_Two() + { + // 22 查询并填充默认值 如果数组中存在null值,用默认值0替换。 + + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + // ?? 是一个空合并运算符。它用于为可空值类型和引用类型提供一个默认值,当操作数的值为 null 时。 + var num =nullableNumbers.Select(n=>n??0); + + return View(num); + } + public IActionResult Twenty_Three() + { + // 23 查询并转换类型 将字符串数组转换为整数数组。 + + string[] stringNumbers = { "1", "2", "3", "4" }; + // 将string[]转换为int[]数组 + // 转换函数使用Lambda表达式 n=>int.Parse(n),它将每个字符串转换为整数。 使用.ToArray() + // Parse 是一个静态方法,用于将字符串类型的表示形式转换为等效的数值类型 + int[] intnumber = stringNumbers.Select(n=>int.Parse(n)).ToArray(); + + return View(); + } + +public class Student +{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} +public class Course +{ + public int StudentId{get;set;} + public string CourseName{get;set;}=null!; + +} +``` +### 3.高级 +```js + public IActionResult Twenty_Four() + { + // 24 查询并使用 OfType<数据类型>() 过滤 从对象数组中过滤出字符串类型的元素。 + + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType().ToList(); + + return View(result); + } + public IActionResult Twenty_Five() + { + // 25 查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + // var num = Zip(numbers1,numbers2).ToArray(); + return View(); + } + public IActionResult Twenty_Six() + { + // 26 查询并使用Range生成 生成一个包含1到10的整数数组。 + int[] numbers = Enumerable.Range(1, 10).ToArray(); + + return View(numbers); + } + public IActionResult Twenty_Seven() + { + // 27 查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + // Enumerable.Repeat(数字, 重复的次数) + int[] numbers = Enumerable.Repeat(7, 5).ToArray(); //77777 + + return View(numbers); + } + public IActionResult Twenty_Eight() + { + // 28 查询并使用Take限制数量 从数组中取出前5个元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Take(5); + return View(num); + } + public IActionResult Twenty_Nine() + { + // 29 查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Skip(3); + return View(num); + } + + public class Student +{ + public int Id{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} +public class Course +{ + public int StudentId{get;set;} + public string CourseName{get;set;}=null!; + +} +``` \ No newline at end of file -- Gitee