From c97dccec1ea6c8b41c203e3638f4f66a85a5dfaa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E9=9B=A8=E6=B9=98?= <2488028707@qq.com> Date: Sun, 8 Dec 2024 20:02:05 +0800 Subject: [PATCH 1/4] 20241202 --- ...45\210\240\346\224\271\346\237\245CRUD.md" | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 "\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245CRUD.md" diff --git "a/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245CRUD.md" "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245CRUD.md" new file mode 100644 index 0000000..d4ebd2c --- /dev/null +++ "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241202-\345\242\236\345\210\240\346\224\271\346\237\245CRUD.md" @@ -0,0 +1,25 @@ +1. 关于高的 + - height:calc(100% - 90px); +2. 静态的一个方法? + - public static List Blogs{get;set;} +3. 构造函数,初始化 + - static Db(){Blogs = new List();} + - static Db(){Blogs = [];} + +### 上传的操作 +1. 回到MVC文件外 + - cd.. +2. 初始化一个仓库 + git init +3. git add . +4. git commit -m "备注" +5. git remote add origin 复制的gitee地址 +6. git push -u origin master +7. git remote remove orgin(如果之前有弄错,可以用这个来移除) + +### 如何运行 +1. 克隆到本地:git clone https://gitee.com/myhfw003/grade-23-class-4-mvc-demo.git +2. 需要安装SDK,项目基于net8.0,请安装对应版本 +3. cd ./Blog +4. dotnet watch +5. 访问:http://localhost:5000/blogs/index \ No newline at end of file -- Gitee From 10b7512767931ad86985236b9014f046f50c9882 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E9=9B=A8=E6=B9=98?= <2488028707@qq.com> Date: Sun, 8 Dec 2024 20:02:32 +0800 Subject: [PATCH 2/4] 20241204 --- ...06\346\210\220\346\237\245\350\257\242.md" | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) create mode 100644 "\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" diff --git "a/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" new file mode 100644 index 0000000..d48928a --- /dev/null +++ "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241204-Linq\351\233\206\346\210\220\346\237\245\350\257\242.md" @@ -0,0 +1,19 @@ +## 三、Linq集成查询(关联Lambda) + +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}对象的形式,返回新的集合 -- Gitee From 869eddb1b39500edf048b1c2692b966e636a05b2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E9=9B=A8=E6=B9=98?= <2488028707@qq.com> Date: Sun, 8 Dec 2024 20:03:18 +0800 Subject: [PATCH 3/4] 20241205 --- .../Linq\346\237\245\350\257\242.cs" | 125 ++++++++++++++++++ ...71\346\237\245\345\256\214\345\226\204.md" | 77 +++++++++++ 2 files changed, 202 insertions(+) create mode 100644 "\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" create mode 100644 "\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\345\256\214\345\226\204.md" diff --git "a/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" "b/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" new file mode 100644 index 0000000..588104b --- /dev/null +++ "b/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" @@ -0,0 +1,125 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Linq查询 +{ + public class Student + { + public int Id { get; set; } + public string Name { get; set; } + public int Age { get; set; } + } + internal class Program + { + static void Main(string[] args) + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + //1.找出数组中等于5的元素 + var a = numbers.Where(x => x == 5); + Console.WriteLine("第一题:"); + foreach (var num in a) + { + Console.Write(num + " "); + } + Console.WriteLine(); + //2.找出数组中在2到8之间的元素。 + var b = numbers.Where(x => x > 2 && x < 8); + Console.WriteLine("第二题:"); + foreach (var num in b) + { + Console.Write(num + " "); + } + Console.WriteLine(); + //3.将数组中的每个数字乘以2。 + var c = numbers.Select(x => x * 2); + Console.WriteLine("第三题:"); + foreach (var num in c) + { + Console.Write(num + " "); + } + Console.WriteLine(); + + + 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 }, + }; + //4.找出所有名字以"王"开头的学生。 + var d = students.Where(x => x.Name.StartsWith("王")).ToList(); + Console.WriteLine("名字以'王'开头的学生:"); + foreach (var student in d) + { + Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Age: {student.Age}"); + } + //5.找出所有年龄大于20岁的学生,并按年龄降序排列。 + var e = students.Where(x => x.Age > 20).OrderByDescending(x => x.Age) + .ToList(); + Console.WriteLine("第五题"); + foreach (var student in e) + { + Console.WriteLine($"Id: {student.Id}, Name: {student.Name}, Age: {student.Age}"); + } + int[] numbers1 = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + //6.找出数组中所有不重复的数字。 + var f = numbers1.Distinct(); + Console.WriteLine("所有不重复的数字:"); + foreach (var num in f) + { + Console.Write(num + " "); + } + Console.WriteLine(); + //7.找出数组中第一个大于3的元素。 + int g = numbers1.First(x => x > 3); + Console.WriteLine("第一个大于3的元素:" + g); + //8.找出数组中最后一个小于7的元素。 + int h = numbers1.Last(x => x < 7); + Console.WriteLine("最后一个小于7的元素:" + h); + //9.检查数组中是否存在大于10的元素。 + bool i = numbers1.Any(x => x > 10); + if (i) + { + Console.WriteLine("数组中存在大于10的元素"); + } + else + { + Console.WriteLine("数组中不存在大于10的元素"); + } + + //10.计算数组中大于5的元素数量。 + int j = numbers1.Count(x => x > 5); + Console.WriteLine("大于5的元素数量:" + j); + //11.计算数组中所有元素的总和。 + int k = numbers1.Sum(); + Console.WriteLine("所有元素的总和:"+k); + //12.找出数组中的最大值。 + int l = numbers1.Max(); + Console.WriteLine("最大值:" + l); + //13.找出数组中的最小值。 + int m = numbers1.Min(); + Console.WriteLine("最小值:" + m); + //14.计算数组中所有元素的平均值。 + double n = numbers1.Count(); + Console.WriteLine("平均值:" + n); + //15.找出数组中能被3整除的元素。 + var o = numbers1.Where(x => x % 3 == 0).Distinct(); + Console.WriteLine("被3整除的元素:"); + foreach(var num in o) + { + Console.Write(num+ " "); + } + + Console.Read(); + } + } + +} diff --git "a/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\345\256\214\345\226\204.md" "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\345\256\214\345\226\204.md" new file mode 100644 index 0000000..5717ce6 --- /dev/null +++ "b/\351\202\223\351\233\250\346\271\230/\347\254\224\350\256\260/20241205-\345\242\236\345\210\240\346\224\271\346\237\245\345\256\214\345\226\204.md" @@ -0,0 +1,77 @@ +### 删除 +在Controller写 +``` + 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(); + } +``` + +在View写 +``` +@model Blog.Models.Blogs; + +

是否要删除以下信息?

+ + + + + + + + + + + + + + + + + + +
标题:@Model.Title
内容:@Model.Content
作者:@Model.Author
删除我再想想
+``` + +### 查询 +在Controller写 +``` + 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); + } + +``` + +在View写 +``` +
+ + + + +
+``` + +## 作业 +![](https://gitee.com/dengyxiang/picture/raw/master/zy.png) \ No newline at end of file -- Gitee From 253e0f38152c1bc85263e0b027808498d324e93a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=82=93=E9=9B=A8=E6=B9=98?= <2488028707@qq.com> Date: Sun, 8 Dec 2024 20:18:53 +0800 Subject: [PATCH 4/4] 20241204 --- .../Linq\346\237\245\350\257\242.cs" | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git "a/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" "b/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" index 588104b..9162efb 100644 --- "a/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" +++ "b/\351\202\223\351\233\250\346\271\230/\344\275\234\344\270\232/Linq\346\237\245\350\257\242.cs" @@ -119,6 +119,53 @@ namespace Linq查询 } Console.Read(); + // 16.找出所有学生的姓名和年龄。 + var p = students.Select(x => new { x.Name, x.Age }).ToList(); + // 17.年龄分组学生,并计算每个年龄组的学生数量。 + var q = students.GroupBy(s => s.Age).Select(g => new { Age = g.Key, Count = g.Count() }).ToList(); ; + + 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="语文"}, + }; + //18.联结学生和课程表,找出每个学生的所有课程 + var r = from student in students + join course in courses on student.Id equals course.StudentId into studentCourses + select new { StudentName = student.Name, Courses = studentCourses.Select(c => c.CourseName).ToList() }; + + //19.反转数组中的元素顺序。 + var s = numbers1.AsEnumerable().Reverse(); + //20.找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + int t = numbers.FirstOrDefault(n => n > 2); + //21.从数组中排除所有小于5的元素。 + var u = numbers.Where(n => n >= 5).ToList(); + //22. 如果数组中存在null值,用默认值0替换。 + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + var v = nullableNumbers.Select(n => n ?? 0).ToList(); + //23. 将字符串数组转换为整数数组。 + string[] stringNumbers = { "1", "2", "3", "4" }; + int[] w = stringNumbers.Select(int.Parse).ToArray(); + //24.从对象数组中过滤出字符串类型的元素。 + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType().ToList(); + + //25.合并两个数组,并创建一个包含元素对的新数组。 + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + var x = numbers1.Zip(numbers2, (first, second) => new { first, second }).ToList(); + //26.生成一个包含1到10的整数数组。 + int[] y = Enumerable.Range(1, 10).ToArray(); + //27. 重复一个元素多次,创建一个新数组。 + //28.从数组中取出前5个元素。 + var z = numbers1.Take(5).ToArray(); + //29.跳过数组中的前3个元素,然后取出剩余的元素。 + var aa = numbers.Skip(3).ToArray(); s } } -- Gitee