From e8dc9252c858c440d9987c61a115d135359a62f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=90=B4=E5=98=89=E7=8E=B2?= <14091858+softwork-wjl@user.noreply.gitee.com> Date: Sun, 15 Dec 2024 18:36:36 +0800 Subject: [PATCH] 1209-1212 --- ...45\346\225\260\346\215\256\345\272\223.md" | 70 +++++ .../20241212linq29.md" | 245 ++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 "\345\220\264\345\230\211\347\216\262/20241209\351\223\276\346\216\245\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\220\264\345\230\211\347\216\262/20241212linq29.md" diff --git "a/\345\220\264\345\230\211\347\216\262/20241209\351\223\276\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\345\220\264\345\230\211\347\216\262/20241209\351\223\276\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..e6132d8 --- /dev/null +++ "b/\345\220\264\345\230\211\347\216\262/20241209\351\223\276\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,70 @@ +# 数据模型 +## 数据库的选择和权衡 + + + Sqlserver + + PostgreSQL + + Mysql/MariaDb + + Oracle + + Db2 + + Sybase + +## ORM工具的选择和使用 + + + EntityFrameworkCore + + Dapper + + SqlSuper + +# 数据的持久化 + - 结绳记事 + - 甲骨文 + - 木简 + - 竹简 + - 纸 + - 账本 + - 文本文件、Excel (磁盘:HDD、SSD) + - 数据库 + - 常见数据库 + - Sqlserver + - PostgreSQL + - Mysql/MariaDb - + - 常见ORM工具(比喻为应用和数据库之间的通讯员) + - Dapper + - EntityFrameworkCore + - FreeSql + + +# 应用EntityFrameworkCore的步骤 +1. 安装依赖包,命令:dotnet add package Microsoft.EntityFrameworkCore.SqlServer +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/\345\220\264\345\230\211\347\216\262/20241212linq29.md" "b/\345\220\264\345\230\211\347\216\262/20241212linq29.md" new file mode 100644 index 0000000..7b29fc8 --- /dev/null +++ "b/\345\220\264\345\230\211\347\216\262/20241212linq29.md" @@ -0,0 +1,245 @@ +1. 第1-3题 + ``` + public IActionResult Index() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + //1.查询特定元素 找出数组中等于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 Index() + { + //查询特定属性的对象 找出所有名字以"王"开头的学生。 + 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 Index() + { + 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 Index() + { + //查询并去重 找出数组中所有不重复的数字。 + 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(); + } + ``` +5. 第16-17题 + ``` + public IActionResult Index() + { + 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 }, + }; + //查询并选择特定属性 找出所有学生的姓名和年龄 + var a = students.Select(x => new{x.Name,x.Age}).ToList(); + //查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + var b=students.GroupBy(s => s.Age) .Select(g => new { Age = g.Key, Count = g.Count() }) .ToList();; + return View(); + } + ``` +6. 第18题 + ``` + public IActionResult Index() + { + 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="语文"}, + }; + //查询并联结 联结学生和课程表,找出每个学生的所有课程。 + 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()); + + } + ``` +7. 第19-21题 + ``` + public IActionResult Index() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + //查询并反转 反转数组中的元素顺序。 + var a = numbers.AsEnumerable().Reverse(); + // 将结果转换回数组 + int[] four = a.ToArray(); + //查询并填充 找出数组中第一个大于2的元素,并用它填充后面的所有位置。 + int b = numbers.FirstOrDefault(n => n > 2); + // 如果找到了,用它填充后面的所有位置 + if (b != 0) // 这里使用0是因为b是int类型,它的默认值是0 + { + int index = Array.IndexOf(numbers, b); + for (int i = index + 1; i < numbers.Length; i++) + { + numbers[i] = b; + } + } + //查询并排除 从数组中排除所有小于5的元素。 + var c = numbers.Where(n => n >= 5).ToList(); + return View(); + + } + ``` +8. 第22题 + ``` + public IActionResult Index() + { + int?[] nullableNumbers = { 1, null, 3, null, 5 }; + //查询并填充默认值 如果数组中存在null值,用默认值0替换。 + var a = nullableNumbers.Select(n => n ?? 0).ToList(); + return View(); + } + ``` +9. 第23题 + ``` + public IActionResult Index() + { + string[] stringNumbers = { "1", "2", "3", "4" }; + //查询并转换类型 将字符串数组转换为整数数组。 + int[] a = stringNumbers.Select(int.Parse).ToArray(); + return View(); + } + ``` +10. 第24题 + ``` + public IActionResult Index() + { + object[] objects = { "String", 123, "Another String", 456 }; + var result = objects.OfType().ToList(); + //查询并使用OfType过滤 从对象数组中过滤出字符串类型的元素。 + return View(); + } + ``` +11. 第25题 + ``` + public IActionResult Index() + { + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + //查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 + var a = numbers1.Zip(numbers2, (first, second) => new { first, second }).ToList(); + return View(); + } + ``` +12. 第26题 + ``` + public IActionResult Index(){ + //查询并使用Range生成 生成一个包含1到10的整数数组 + int[] a = Enumerable.Range(1, 10).ToArray(); + return View(a); + } + ``` +13. 第27题 + ``` + public IActionResult Index(){ + //查询并使用Repeat重复 重复一个元素多次,创建一个新数组。 + // 要重复的元素 + int number = 7; + // 重复的次数 + int repeatCount = 10; + // 使用LINQ的Repeat方法重复元素 + var a = Enumerable.Repeat(number, repeatCount).ToArray(); + // 将结果传递给视图 + return View(); + } + ``` +14. 第28题 + ``` + public IActionResult Index(){ + //查询并使用Take限制数量 从数组中取出前5个元素。 + 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(); + } + ``` +15. 第29题 + ``` + public IActionResult Index(){ + //查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 + 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