From 6baacff5866a559b05345fd189fefc4c18775744 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BA=B7=E7=AB=A5=E9=9E=8B?= <1431529528@qq.com> Date: Sun, 8 Dec 2024 22:11:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=8F=90=E4=BA=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\241\200\347\273\203\344\271\240.md" | 173 ++++++++++++++++++ 1 file changed, 173 insertions(+) create mode 100644 "\345\272\267\345\273\272\346\242\205/20241202--\347\274\226\350\276\221\346\223\215\344\275\234\343\200\201\345\237\272\347\241\200\347\273\203\344\271\240.md" diff --git "a/\345\272\267\345\273\272\346\242\205/20241202--\347\274\226\350\276\221\346\223\215\344\275\234\343\200\201\345\237\272\347\241\200\347\273\203\344\271\240.md" "b/\345\272\267\345\273\272\346\242\205/20241202--\347\274\226\350\276\221\346\223\215\344\275\234\343\200\201\345\237\272\347\241\200\347\273\203\344\271\240.md" new file mode 100644 index 0000000..718a968 --- /dev/null +++ "b/\345\272\267\345\273\272\346\242\205/20241202--\347\274\226\350\276\221\346\223\215\344\275\234\343\200\201\345\237\272\347\241\200\347\273\203\344\271\240.md" @@ -0,0 +1,173 @@ +# 笔记 + +## 编辑操作 + +### 在控制器中 +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); + } + ``` + 对应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:没有框框 +* 空值:有框框,但没值(里面没有加入内容) + + + + +# 作业 + +## 基础练习 + +1. 查询特定元素 找出数组中等于5的元素。 +```C# +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Linq.Models; + +int[] numbers = { 1, 2, 3, 4, 5, 6 }; +namespace Linq.Controllers; + +where(x=>x.numbers=5) +\ No newline at end of file + +public class Linqs:Controller +{ + // 1.找出数组中等于5的元素 + public ActionResult Index_1() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var a=numbers.Where(x=>x==5); + return View(a); + } + + // 2.找出数组中在2到8之间的元素 + public ActionResult Index_2() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 9 }; + var a=numbers.Where(x=>x>=2 && x<=8); + return View(a); + } + //3.将数组中的每个数字乘以2。 + public ActionResult Index_3() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 9 }; + var a=numbers.Select(x=>x*2); + return View(a); + } + + // 4.找出所有名字以"王"开头的学生 + public ActionResult Index_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 }, + }; + //5.检查字符串是否以指定的前缀开始.StartsWith() + var a=students.Where(x=>x.Name.StartsWith("王")); + return View(a); + } + + // 6.找出所有年龄大于20岁的学生,并按年龄降序排列。 + public ActionResult Index_5() + { + 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() + var a=students.Where(x=>x.Age>20).OrderByDescending(x=>x.Age); + return View(a); + + } + // 7.查询并去重 找出数组中所有不重复的数字 + public ActionResult Index_6() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a = numbers + // GroupBy(n => n):将数组中的数字按照值进行分组。 + .GroupBy(n => n) + // Where(g => g.Count() == 1):筛选出只出现一次的组。 + .Where(g => g.Count() == 1) + // Select(g => g.Key):选择这些组的键,即只出现一次的数字。 + .Select(g => g.Key); + return View(a); + } + + // 8. + + +} +``` \ No newline at end of file -- Gitee