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 0000000000000000000000000000000000000000..d78a713ffb7ffb7b3b49bd610bec5a7b0b33a20f --- /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,87 @@ +```C# +using System.Diagnostics; +using Microsoft.AspNetCore.Mvc; +using Linq.Models; + +namespace Linq.Controllers; + + +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 }, + }; + //检查字符串是否以指定的前缀开始.StartsWith() + var a=students.Where(x=>x.Name.StartsWith("王")); + return View(a); + } + + // 5.找出所有年龄大于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); + + } + // 6.查询并去重 找出数组中所有不重复的数字 + 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); + } + + // 7. + + +} +``` \ No newline at end of file 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 0000000000000000000000000000000000000000..bab017d07c68c706f3952b3affaae5d71cb3e131 --- /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,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
确定删除 我在想想
+``` +## 改 +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 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 0000000000000000000000000000000000000000..504b4afa1987dd6216a1641fc3a38fc348b6ce49 --- /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 0000000000000000000000000000000000000000..151b0b8a556536c64d530ebffbea9b918fe9d062 --- /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 0000000000000000000000000000000000000000..5fb13cad5649291be33925b8b91a427e1387038c --- /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,77 @@ +## 删除 +控制器 +```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
确定删除 我在想想
+``` + +## 查询 + +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