From ccff9ebf3a61f76e57c8ac841cf9bb99d1e55bfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E7=8C=AA?= <3284280335@qq.com> Date: Thu, 5 Dec 2024 12:02:54 +0800 Subject: [PATCH 1/4] =?UTF-8?q?20241205-=E5=88=A0=E9=99=A4=E3=80=81?= =?UTF-8?q?=E6=9F=A5=E8=AF=A2=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...da\350\241\250\350\276\276\345\274\217.md" | 5 ++ ...36\345\210\240\346\224\271\346\237\245.md" | 1 + ...47\250\213\345\272\217\343\200\201Linq.md" | 43 ++++++++++++++++ ...45\350\257\242\345\212\237\350\203\275.md" | 51 +++++++++++++++++++ 4 files changed, 100 insertions(+) create mode 100644 "\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-\350\241\250\345\215\225\346\240\207\350\256\260\346\216\247\345\210\266\347\250\213\345\272\217\343\200\201Linq.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000..a7438f3 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,5 @@ +1. 查询特定元素 找出数组中等于5的元素。 + +int[] numbers = { 1, 2, 3, 4, 5, 6 }; + +where(x=>x.numbers=5) \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000..504b4af --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245.md" @@ -0,0 +1 @@ +具体去新MVC仓库里面看同日期的Blog源码(带注释版) \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-\350\241\250\345\215\225\346\240\207\350\256\260\346\216\247\345\210\266\347\250\213\345\272\217\343\200\201Linq.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-\350\241\250\345\215\225\346\240\207\350\256\260\346\216\247\345\210\266\347\250\213\345\272\217\343\200\201Linq.md" new file mode 100644 index 0000000..151b0b8 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241204-\350\241\250\345\215\225\346\240\207\350\256\260\346\216\247\345\210\266\347\250\213\345\272\217\343\200\201Linq.md" @@ -0,0 +1,43 @@ + +## Linq集成查询 +1. First FirstOrDefaualt 找第一个符合条件的元素 + - First(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,报错 + - FirstOrDefault(x=>x.Id==id) 返回第一个Id等于id的元素,如果都没有符合的,返回Null +2. Single SingleOrDefault + - Single() 返回第一个元素,如果没有,报错 + - SingleOrDefault() 返回第一个元素,如果没有,返回Null +3. Where + - Where(x=>x.Score>=80 && x.Sex==1) 查找所有成绩大于等于80,并且性别为1的所有元素 +4. Select + - Select(x=>new {x.Id,x.Score}) 以新的{x.Id,x.Score}对象的形式,返回新的集合 +``` +from XX in XX where XXX select; +``` + +## 表单标记帮助程序 +```cshtml +
+``` + +生成以下 HTML: +```html +
+``` + +## 提交到控制器示例 +选中输入或按钮时,下面的标记将窗体提交到 HomeController 的 Index 操作: +```cshtml +
+ + +
+``` + +生成以下 HTML: +```html +
+ + +
+``` \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" new file mode 100644 index 0000000..78efc52 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" @@ -0,0 +1,51 @@ +## 删除 +控制器 +```c# + public IActionResult Delete(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = DB.B.FirstOrDefault(x => x.Id == id); + // 把数据传到视图 + return View(blog); + } + + public IActionResult Deletes(int id) + { + var blog = DB.B.FirstOrDefault(x => x.Id == id); + if(blog!=null) + { + DB.B.Remove(blog); + return RedirectToAction("Index"); + } + return NotFound(); + } +``` +视图 +```html +@model Blog.Models.Blogs; + + + + + + + + + + + + + + + + + + + + + + +
标题@Model.Title
内容@Model.Content
作者@Model.Author
确定删除 我在想想
+``` + +## 查询 \ No newline at end of file -- Gitee From 5693d990f56313aa26781d75a547719a943b3831 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Sun, 8 Dec 2024 20:18:08 +0800 Subject: [PATCH 2/4] =?UTF-8?q?20241208-=E4=BD=9C=E4=B8=9A=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...5\220\210\347\273\203\344\271\240-CRUD.md" | 5 ++++ ...45\350\257\242\345\212\237\350\203\275.md" | 28 ++++++++++++++++++- 2 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 "\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" new file mode 100644 index 0000000..725f15e --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" @@ -0,0 +1,5 @@ +## 增 + +## 删 +## 改 +## 查 \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" index 78efc52..5fb13ca 100644 --- "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241205-\345\210\240\351\231\244\343\200\201\346\237\245\350\257\242\345\212\237\350\203\275.md" @@ -48,4 +48,30 @@ ``` -## 查询 \ No newline at end of file +## 查询 + +1. 控制器 +```c# + public IActionResult Index(string oi) + { + + + if(string.IsNullOrEmpty(oi)) + { + return View(DB.B); + } + oi=string.IsNullOrEmpty(oi)?"":oi.Trim(); + var a=DB.B.Where(x=>x.Title.Contains(oi)|| x.Content.Contains(oi)|| x.Author.Contains(oi)); + return View(a); + + } +``` +2. 视图 +```html +
+

+ + +

+
+``` \ No newline at end of file -- Gitee From c01d0c090547724b4ae2fa064c8ae7b01c169d0f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Sun, 8 Dec 2024 22:05:09 +0800 Subject: [PATCH 3/4] =?UTF-8?q?20241208-=E4=BD=9C=E4=B8=9A=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...da\350\241\250\350\276\276\345\274\217.md" | 88 ++++++++++++++++++- 1 file changed, 85 insertions(+), 3 deletions(-) diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" index a7438f3..bf0709a 100644 --- "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" @@ -1,5 +1,87 @@ -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 From 436c2bfb7e86420fd243798c1b5cb964bc255c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Sun, 8 Dec 2024 23:59:19 +0800 Subject: [PATCH 4/4] =?UTF-8?q?20241208-=E4=BD=9C=E4=B8=9A=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...da\350\241\250\350\276\276\345\274\217.md" | 8 +- ...5\220\210\347\273\203\344\271\240-CRUD.md" | 147 +++++++++++++++++- 2 files changed, 150 insertions(+), 5 deletions(-) diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" index bf0709a..d78a713 100644 --- "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" @@ -42,12 +42,12 @@ public class Linqs:Controller new Student {Id=4, Name = "詹宇航", Age = 35 }, new Student {Id=5, Name = "郑雨良", Age = 26 }, }; - //5.检查字符串是否以指定的前缀开始.StartsWith() + //检查字符串是否以指定的前缀开始.StartsWith() var a=students.Where(x=>x.Name.StartsWith("王")); return View(a); } - // 6.找出所有年龄大于20岁的学生,并按年龄降序排列。 + // 5.找出所有年龄大于20岁的学生,并按年龄降序排列。 public ActionResult Index_5() { List students = new List @@ -66,7 +66,7 @@ public class Linqs:Controller return View(a); } - // 7.查询并去重 找出数组中所有不重复的数字 + // 6.查询并去重 找出数组中所有不重复的数字 public ActionResult Index_6() { int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; @@ -80,7 +80,7 @@ public class Linqs:Controller return View(a); } - // 8. + // 7. } diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" index 725f15e..bab017d 100644 --- "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\347\273\274\345\220\210\347\273\203\344\271\240-CRUD.md" @@ -1,5 +1,150 @@ ## 增 +1. 控制器 +```C# + public IActionResult Add() + { + return View(); + } + [HttpPost] + public IActionResult Add(Blogs a) + { + var maxId=DB.B.Select(x => x.Id).Max(); + a.Id=maxId+1; + DB.B.Add(a); + return RedirectToAction("Index"); + } + public IActionResult Edit(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = DB.B.FirstOrDefault(x => x.Id == id); + return View(blog); + } +``` +2. 视图 +```html +@model Blog.Models.Blogs; + +
+
+
+
+ +
+``` ## 删 +控制器 +```c# + public IActionResult Delete(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = DB.B.FirstOrDefault(x => x.Id == id); + // 把数据传到视图 + return View(blog); + } + + public IActionResult Deletes(int id) + { + var blog = DB.B.FirstOrDefault(x => x.Id == id); + if(blog!=null) + { + DB.B.Remove(blog); + return RedirectToAction("Index"); + } + return NotFound(); + } +``` +视图 +```html +@model Blog.Models.Blogs; + + + + + + + + + + + + + + + + + + + + + + +
标题@Model.Title
内容@Model.Content
作者@Model.Author
确定删除 我在想想
+``` ## 改 -## 查 \ No newline at end of file +1. 控制器 +```C# + public IActionResult Edit(int id) + { + // 根据id找到对应的blogs,有可能为空 + var blog = DB.B.FirstOrDefault(x => x.Id == id); + return View(blog); + } + + [HttpPost] + public IActionResult Edit(Blogs blogs) + { + + // 判断是否有对应的对象 有可能找得到对应id的记录,也有可能找不到 + // 找到后,修改那个对象,然后保存回数据库,返回列表页 + var b=DB.B.FirstOrDefault(x=>x.Id==blogs.Id); + if(b!=null) + { + b.Title=blogs.Title; + b.Content=blogs.Content; + b.Author=blogs.Author; + } + // 找不到,直接返回列表页 + + return RedirectToAction("Index"); + } + +``` +2. 视图 +```html +@model Blog.Models.Blogs; + +
+
+
+
+
+ +
+``` +## 查 +1. 控制器 +```c# + public IActionResult Index(string oi) + { + + + if(string.IsNullOrEmpty(oi)) + { + return View(DB.B); + } + oi=string.IsNullOrEmpty(oi)?"":oi.Trim(); + var a=DB.B.Where(x=>x.Title.Contains(oi)|| x.Content.Contains(oi)|| x.Author.Contains(oi)); + return View(a); + + } +``` +2. 视图 +```html +
+

+ + +

+
+``` \ No newline at end of file -- Gitee