From 0d20cc5156a8a992b8473d021a05e5caca121036 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=96=B9=E5=87=A4=E4=B8=B9?= <14092403+fang-fengdan@user.noreply.gitee.com> Date: Sun, 8 Dec 2024 19:37:04 +0800 Subject: [PATCH] =?UTF-8?q?=E9=9B=86=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...65\351\235\242\347\274\226\350\276\221.md" | 48 +++ ...06\346\210\220\347\273\203\344\271\240.md" | 389 ++++++++++++++++++ 2 files changed, 437 insertions(+) create mode 100644 "\346\226\271\345\207\244\344\270\271/20241202\351\241\265\351\235\242\347\274\226\350\276\221.md" create mode 100644 "\346\226\271\345\207\244\344\270\271/20241204Linq\351\233\206\346\210\220\347\273\203\344\271\240.md" diff --git "a/\346\226\271\345\207\244\344\270\271/20241202\351\241\265\351\235\242\347\274\226\350\276\221.md" "b/\346\226\271\345\207\244\344\270\271/20241202\351\241\265\351\235\242\347\274\226\350\276\221.md" new file mode 100644 index 0000000..16f4bc4 --- /dev/null +++ "b/\346\226\271\345\207\244\344\270\271/20241202\351\241\265\351\235\242\347\274\226\350\276\221.md" @@ -0,0 +1,48 @@ +## 一.笔记 +### 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] + [ValidateAntiForgeryToken] + public IActionResult Edit(Blue input) + { + if(ModelState.IsValid){ + var black=Db.Blue.FirstOrDefault(x=>x.Id==input.Id); + if(black!=null){ + black.Title=input.Title; + black.Author=input.Author; + black.Content=input.Content; + } + return View(input); + } + return View(); + } + public IActionResult Edit(int id) + { + var list=Db.Blue.Where(x=>x.Id==id&&x.Author=="小小"); + var list1=Db.Blue.Select(x=>new{Xyz=x.Id,Abc=x.Author}); + var black=Db.Blue.FirstOrDefault(x=>x.Id==id); + return View(black); + } +``` \ No newline at end of file diff --git "a/\346\226\271\345\207\244\344\270\271/20241204Linq\351\233\206\346\210\220\347\273\203\344\271\240.md" "b/\346\226\271\345\207\244\344\270\271/20241204Linq\351\233\206\346\210\220\347\273\203\344\271\240.md" new file mode 100644 index 0000000..f23a2f5 --- /dev/null +++ "b/\346\226\271\345\207\244\344\270\271/20241204Linq\351\233\206\346\210\220\347\273\203\344\271\240.md" @@ -0,0 +1,389 @@ +## 一.笔记 +### 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来操作 + 删除 + +``` + +### 代码 +```js +public class LinqsController : Controller +{ + public IActionResult One() + {//1.查询特定元素 找出数组中等于5的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var a1 = numbers.Where(n => n == 5).ToList(); + return View(a1); + } + public IActionResult Two() + { + //2.找出数组中在2到8之间的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var a2 = numbers.Where(n => n >= 2 && n <= 8); + return View(a2) + } + public IActionResult Three() + { + //3.查询并转换元素 将数组中的每个数字乘以2。 + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var a3 = numbers.Select(n => n * 2); + return View(a3) + } + 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 }, + }; + var a4 = students.Where(n => n.Name.StartsWith("王")) + return View(a4) + } + 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 }, +}; + var a5 = students.Where(n => n.Age > 20).OrderByDescending(n => n.Age); + return View(a5); + } + public IActionResult Six() + { + //6查询并去重 找出数组中所有不重复的数字。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var shu = numbers.GroupBy(n => n).ToDictionary(n => n.key, n.Count()); + var a6 = numbers.Where(n => shu[n] != 2); + return View(a6); + } + public IActionResult Seven() + { + //7查询第一个元素 找出数组中第一个大于3的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a7 = numbers.Where(n => n > 3).First(); + return View(a7); + } + public IActionResult Eight() + { + //8查询最后一个元素 找出数组中最后一个小于7的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a8 = numbers.LastOrDefault(n => n < 7); + return View(a8) + } + public IActionResult Nine() + {//9查询元素是否存在 检查数组中是否存在大于10的元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + bool a9 = Array.Exists(numbers, a9 => numbers > 10); + viewBag.nums = a9; + return View(a9) + } + public IActionResult Ten() + {//10查询元素的计数 计算数组中大于5的元素数量。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a10 = numbers.Count(n => n > 5); + return View(a10) + } + public IActionResult Eleven() + { + //11查询元素的总和 计算数组中所有元素的总和。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a11 = numbers.sum(n => n); + return View(a11) + } + + public IActionResult Twelve() + { + //12查询元素的最大值 找出数组中的最大值。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a12 = numbers.Max(n => n); + return View(a12) + } + public IActionResult Thirteen() + { + //13查询元素的最小值 找出数组中的最小值。 + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a13 = numbers.Min(n => n); + return View(a13) + } + public IActionResult Fourteen() + { + //14 查询元素的平均值 计算数组中所有元素的平均值。 + + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a14 = numbers.Average(n => n); + return View(a14) + } + public IActionResult Fifteen() + { + //15查询特定条件的元素 找出数组中能被3整除的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a15 = numbers.Where(n => n % 3 == 0); + return View(a15) + } + + + + + 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 a16 = students.Select(n => new { n.Name, n.Age }).ToList(); + return View(a16) + } + 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 a17 = students.GroupBy(n => n.Age ).Select(n=>new{Age=n.key,Count=n.Count()}); + return View(a17) + } + + + 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 a18=students.Select(students=>new{ + studentId=students.Id, + StudentName=students.Name, + Courses=courses.Where(courses=>courses.StudentId==students.Id).Select(course=>course.CourseName).ToList() + +}).ToList(); +return View(a18) + } + public IActionResult Nineteen() + { + //19 查询并反转 反转数组中的元素顺序。 + + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a19=numbers.Reverse(); + return View(a19) + } + public IActionResult Twenty() + { + //20查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a20=numbers.Where(n=>n>2).Selete(n=>2); + return View(a20) + } + public IActionResult Twenty_One() + { + //21查询并排除 从数组中排除所有小于5的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var a21=numbers.Where(n=>n>5); + return View(a21) + } + public IActionResult Twenty_Two() + { + //22 查询并填充默认值 如果数组中存在null值,用默认值0替换。 + + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + var a22=nullableNumbers.Selete(n=>n??0); + return View(a22) + } + public IActionResult Twenty_Three() + { + //23查询并转换类型 将字符串数组转换为整数数组。 + + string[] stringNumbers = { "1", "2", "3", "4" }; + int a23=stringNumbers.Selete(n=>int.Parse(n)).ToArray(); + return View(a23) + } + + + + public IActionResult Twenty_Four() + { + //24查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + + object[] objects = { "String", 123, "Another String", 456 }; + var a24 = objects.OfType().ToList(); + return View(a24) + } + public IActionResult Twenty_Five() + { + // 25查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + return View() + } + public IActionResult Twenty_Six() + { + // 26 查询并使用Range生成 生成一个包含1到10的整数数组。 + int[] a26 = Enumerable.Range(1, 10).ToArray(); + + return View(a26); + } + public IActionResult Twenty_Seven() + { + // 27 查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + + int[] a27 = Enumerable.Repeat(7, 5).ToArray(); //77777 + + return View(a27); + } + public IActionResult Twenty_Eight() + { + // 28 查询并使用Take限制数量 从数组中取出前5个元素。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a28 = numbers.Take(5); + return View(a28); + } + public IActionResult Twenty_Nine() + { + // 29 查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a29 = numbers.Skip(3); + return View(a29); + } +} +``` \ No newline at end of file -- Gitee