From 2760c0abe9a7df3955c5e75e1ca5d80f141a06e2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=80=9D?= <1662679300@qq.com> Date: Sun, 15 Dec 2024 19:35:02 +0800 Subject: [PATCH] =?UTF-8?q?=E7=AC=94=E8=AE=B0=E7=BB=83=E4=B9=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...56\346\214\201\344\271\205\345\214\226.md" | 61 ++++++++ .../20241212-\346\237\245\350\257\242.md" | 143 ++++++++++++++++++ 2 files changed, 204 insertions(+) create mode 100644 "\346\235\216\346\200\235/20241209-\346\225\260\346\215\256\346\214\201\344\271\205\345\214\226.md" create mode 100644 "\346\235\216\346\200\235/20241212-\346\237\245\350\257\242.md" diff --git "a/\346\235\216\346\200\235/20241209-\346\225\260\346\215\256\346\214\201\344\271\205\345\214\226.md" "b/\346\235\216\346\200\235/20241209-\346\225\260\346\215\256\346\214\201\344\271\205\345\214\226.md" new file mode 100644 index 0000000..2b3b8ae --- /dev/null +++ "b/\346\235\216\346\200\235/20241209-\346\225\260\346\215\256\346\214\201\344\271\205\345\214\226.md" @@ -0,0 +1,61 @@ + +## 数据持久化 + +- 结绳记事 +- 甲骨文 +- 木简 +- 竹简 +- 纸 +- 账本 +- 文本文件、Excel(磁盘:HDD、SDD) +- 数据库 + - 常见数据库 + - SQLserver + - PostgreSQL + - Mysql/MariaDb + - 常见ORM工具(比喻应用和数据库之间的通讯员) + - Dapper + - EntityFrameworkCore + - FreeSql + + +## 应用EntityFrameworkCore的步骤 + +1. 安装依赖包,命令:dotent add package Microsoft.EntityFrameworkCore.SqlSever + +2. 定义数据库表模型 + +``` +namespace Blog.Models; + +public class Blogs +{ + public int Id { get; set; } + public string Title { get; set; } = null!; + public string Content { get; set; } = null!; + public string Author { get; set; } = null!; +} +``` + +3. 定义数据库上下文 + +``` +using Microsoft.EntityFrameworkCore; + +namespace Blog.Models; + +public class BlogDbContext : DbContext +{ + public DbSet Blogs { get; set; } = null!; + + + // The following configures EF to create a Sqlite database file in the + // special "local" folder for your platform. + protected override void OnConfiguring(DbContextOptionsBuilder options) + => options.UseSqlServer($"Server=.\\SQLEXPRESS;database=Blog4;uid=sa;pwd=123456;TrustServerCertificate=True;"); +} +``` + +4. 生成迁移文件,命令:dotnet ef migrations add XXX(PS:可能需要安装如下依赖包:Microsoft.EntityFrameworkCore.Design) + +5. 将上一步生成的迁移文件,更新到数据库:dotnet ef database update (PS:需要保证连接字符串正确无误,包括用户名、密码等,数据库打开,并且允许远程连接) \ No newline at end of file diff --git "a/\346\235\216\346\200\235/20241212-\346\237\245\350\257\242.md" "b/\346\235\216\346\200\235/20241212-\346\237\245\350\257\242.md" new file mode 100644 index 0000000..3fcd58e --- /dev/null +++ "b/\346\235\216\346\200\235/20241212-\346\237\245\350\257\242.md" @@ -0,0 +1,143 @@ + +``` +public dynamic 名字(){ + +} 可以直接返回 +``` + +All() 所有的都符合条件的返回true,有一个不符合的就返回false + +Any() 只要有就返回true + + + +``` +public IActionResult One() + { + 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 }, + }; + 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="语文"}, + }; + //16.查询并选择特定属性 找出所有学生的姓名和年龄 + var a = students.Select(x => new {x.Name,x.Age}); + var res = stujdents.Select(x => { + var tmpName = x.Age <= 22 ? $"{x.Name}小朋友":$"{x.Name}先生"; + var tmpAge = x.Age + 1; + return new {Name = tmpName,Age = tmpAge}; + }); + return res; + //17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + var b=students.GroupBy(s => s.Age) .Select(g => new { Age = g.Key, Count = g.Count()}); + return View(); + //18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + var a = 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()}; + return View(a.ToList()); + } + public IActionResult Three() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //19.查询并反转 反转数组中的元素顺序。 + var a = numbers.AsEnumerable().Reverse(); + int[] four = a.ToArray(); + //20.查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + int b = numbers.FirstOrDefault(n => n > 2); + if (b != 0) + { + int index = Array.IndexOf(numbers, b); + for (int i = index + 1; i < numbers.Length; i++) + { + numbers[i] = b; + } + } + //21.查询并排除 从数组中排除所有小于5的元素。 + var c = numbers.Where(n => n >= 5); + return View(); + } + //22.查询并填充默认值 如果数组中存在null值,用默认值0替换。 + public IActionResult Four() + { + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + + var a = nullableNumbers.Select(n => n ?? 0); + return View(); + } + //23.查询并转换类型 将字符串数组转换为整数数组。 + public IActionResult Five() + { + string[] stringNumbers = { "1", "2", "3", "4" }; + + int[] a = stringNumbers.Select(int.Parse).ToArray(); + return View(); + } + //24.查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + public IActionResult Six() + { + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType(); + + return View(); + } +``` + + +``` +//25.查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + public IActionResult One() + { + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + + var a = numbers1.Zip(numbers2, (first, second) => new { first, second }); + return View(); + } + //26.查询并使用Range生成 生成一个包含1到10的整数数组 + public IActionResult Two(){ + + int[] a = Enumerable.Range(1, 10).ToArray(); + return View(a); + } + public IActionResult Three(){ + //27.查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + // 重复的元素 + int number = 7; + // 重复的次数 + int repeatCount = 10; + // 使用LINQ的Repeat方法重复元素 + var a = Enumerable.Repeat(number, repeatCount).ToArray(); + return View(); + } + //28.查询并使用Take限制数量 从数组中取出前5个元素。 + public IActionResult Four(){ + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Take方法取出前5个元素 + var a = numbers.Take(5).ToArray(); + return View(); + } + //29.查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + public IActionResult Five(){ + + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + // 使用LINQ的Skip方法跳过前3个元素 + var a = numbers.Skip(3).ToArray(); + return View(); + } +``` \ No newline at end of file -- Gitee