diff --git "a/\350\202\226\347\277\224/\344\275\234\344\270\232/\351\232\217\346\234\272\345\220\215\345\255\227.md" "b/\350\202\226\347\277\224/\344\275\234\344\270\232/2024-1128-\351\232\217\346\234\272\345\220\215\345\255\227.md" similarity index 100% rename from "\350\202\226\347\277\224/\344\275\234\344\270\232/\351\232\217\346\234\272\345\220\215\345\255\227.md" rename to "\350\202\226\347\277\224/\344\275\234\344\270\232/2024-1128-\351\232\217\346\234\272\345\220\215\345\255\227.md" diff --git "a/\350\202\226\347\277\224/\344\275\234\344\270\232/2024-1205-linq\351\233\206\346\210\220\346\237\245\350\257\242\347\273\203\344\271\240.md" "b/\350\202\226\347\277\224/\344\275\234\344\270\232/2024-1205-linq\351\233\206\346\210\220\346\237\245\350\257\242\347\273\203\344\271\240.md" new file mode 100644 index 0000000000000000000000000000000000000000..057983f41e844ed2cc793106e4b94df0530e47fc --- /dev/null +++ "b/\350\202\226\347\277\224/\344\275\234\344\270\232/2024-1205-linq\351\233\206\346\210\220\346\237\245\350\257\242\347\273\203\344\271\240.md" @@ -0,0 +1,80 @@ +1. 1-3 + ``` + public IActionResult One() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + //查询特定元素 找出数组中等于5的元素。 + var one = numbers.Where(n => n == 5).ToList(); + //查询特定范围的元素 找出数组中在2到8之间的元素 + var two = numbers.Where(n => n >2 && n<8).ToList(); + //查询并转换元素 将数组中的每个数字乘以2。 + var three=numbers.Select(n=>n*2).ToList(); + return View(); + } + ``` +2. 4 + ``` + public IActionResult Two() + { + //查询特定属性的对象 找出所有名字以"王"开头的学生。 + 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 four=students.Where(n=>n.Name.Contains("王")).ToList(); + return View(four); + } + ``` +3. 5 + ``` + public IActionResult Three() + { + List students = new List + { + //查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + 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 five=students.Where(n=>n.Age>=20).OrderByDescending(s => s.Age).ToList(); + return View(five); + } + ``` +4. 6-15 + ``` + public IActionResult Four() + { + //查询并去重 找出数组中所有不重复的数字。 + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a=numbers.Distinct().ToList(); + //查询第一个元素 找出数组中第一个大于3的元素。 + var b=numbers.FirstOrDefault(x=>x>3); + //查询最后一个元素 找出数组中最后一个小于7的元素。 + var c=numbers.LastOrDefault(x=>x<7); + //查询元素是否存在 检查数组中是否存在大于10的元素 + var d=numbers.Any(x=>x>10); + //查询元素的计数 计算数组中大于5的元素数量。 + var e=numbers.Count(x=>x>5); + //查询元素的总和 计算数组中所有元素的总和。 + var f=numbers.Sum(); + //查询元素的最大值 找出数组中的最大值。 + var g=numbers.Max(); + //查询元素的最小值 找出数组中的最小值。 + var h=numbers.Min(); + //查询元素的平均值 计算数组中所有元素的平均值。 + //double average = numbers.Average(); + var i=numbers.Sum()/numbers.Length; + //查询特定条件的元素 找出数组中能被3整除的元素。 + var j=numbers.Where(n => n % 3 == 0).ToList(); + return View(); + } + ``` \ No newline at end of file diff --git "a/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1202-\345\242\236\345\210\240\346\224\271\346\237\245.md" "b/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1202-\345\242\236\345\210\240\346\224\271\346\237\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1204-\345\210\240\351\231\244.md" "b/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1204-\345\210\240\351\231\244.md" new file mode 100644 index 0000000000000000000000000000000000000000..0ae8bdbf29e666a9fa28a030ea42f869af13bc9e --- /dev/null +++ "b/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1204-\345\210\240\351\231\244.md" @@ -0,0 +1,71 @@ +### 页面删除功能 +1. 控制台 +``` + public IActionResult Delete(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.ID == id); + return View(blog); + } + + public IActionResult DeleteConfirm(int id) + { + var blog = Db.Blogs.FirstOrDefault(x => x.ID == id); + if (blog != null) + { + Db.Blogs.Remove(blog); + return RedirectToAction("Index"); + } + return NotFound(); + } +``` +2. 视图 +``` +@model Blog.Models.Blogs; + +

是否要删除以下信息?

+ + + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
删除我再想想
+``` +### 页面查询功能 +1. 控制台 +``` + public IActionResult Index(string keyword) + { + keyword = string.IsNullOrEmpty(keyword) ? "" : keyword.Trim(); + if (string.IsNullOrEmpty(keyword)) + { + return View(Db.Blogs); + } + + var list = Db.Blogs.Where(x => x.Title.Contains(keyword) + || x.Content.Contains(keyword) || x.Author.Contains(keyword)).ToList(); + return View(list); + } + +``` +2. Index视图 +``` +
+ + + + +
+``` \ No newline at end of file diff --git "a/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1205-\351\233\206\346\210\220\346\237\245\350\257\242.md" "b/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1205-\351\233\206\346\210\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000000000000000000000000000000000000..bd1b9abdcf5b03099a12419b4d80f4c3764817db --- /dev/null +++ "b/\350\202\226\347\277\224/\347\254\224\350\256\260/2024-1205-\351\233\206\346\210\220\346\237\245\350\257\242.md" @@ -0,0 +1,22 @@ +### 提交信息的组件 +``` +@model Blog.Models.Blogs; + +
+
+
+
+ +
+``` +### Linq集成查询(关联Lambda) +1. First FirstOrDefault +- 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=>Score>=80 && x.Sex==1) 查找所有成绩大于等于80,并且性别为1的所有元素 +4. Select +- Select(x=>new{x.Id,x.Score}) 以新的{x.Id,x.Score}对象的形式,返回新的集合 \ No newline at end of file