From 3e1bb401483ad3f0ed9fd3c4e2659808889dd7d7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=88=98=E6=AC=A2?= <3167389063@qq.com> Date: Wed, 11 Dec 2024 22:45:52 +0800 Subject: [PATCH] =?UTF-8?q?20241211=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...45\346\225\260\346\215\256\345\272\223.md" | 76 +++++++ ...20241211--Linq\347\273\203\344\271\240.md" | 214 ++++++++++++++++++ 2 files changed, 290 insertions(+) create mode 100644 "\345\210\230\346\254\242/20241209--\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\345\210\230\346\254\242/20241211--Linq\347\273\203\344\271\240.md" diff --git "a/\345\210\230\346\254\242/20241209--\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" "b/\345\210\230\346\254\242/20241209--\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..d62319a --- /dev/null +++ "b/\345\210\230\346\254\242/20241209--\350\277\236\346\216\245\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,76 @@ +## 持久化及持久化方案 +- 结绳记事 +- 甲骨文 +- 木简 +- 竹简 +- 碑 +- 纸 +- 账本 +- 电子的文件,如文本文件、Excel、Word +- 数据库 + - 市面上常用数据库 + - Sqlserver 微软的产品 + - PostgreSQl 开源产品 社区驱动 + - MySQL/MariaDb 开源产品 公司驱动 + - Oracle 商业产品 + - Sysbase 商业产品 + - DB2 商业产品 + - 达梦 + - 人大金仓 + - Redis 非关系型数据库 开源 + - Memarchem 非关系型数据库 开源 + - MongoDb 非关系型数据库 开源 + - 常用的ORM工具(应用和数据库打交道的工具) + - Dapper 开源产品 特点是速度快,但代码写起来很麻烦 + - EntityFrameworkCore 微软家产品 特点是代码写起来很清楚并且快,但执行速度较慢(相对于Dapper + - 数据库优先 曾经很流行,现在不怎么流行 + - 代码优先 现在比较主流的做法 + - 定义数据库模型 + - 生成迁移文件(执行一个命令) + - 将迁移文件同步到数据库,完成 + - FreeSql 开源 + - Hibernet 开源 + +## Models数据库类型 +- 模型中的每一个类型,都和数据库表中的数据库一一对应 +- 一条数据表中的记录,在程序或应用中表现为一个对象 +- 一系列记录,则在程序和应用中表现为一个集合 + + +## 连接数据库(代码优先) + +1. 定义数据库模型 +```cs +using Microsoft.EntityFrameworkCore; +namespace Apple.Models; + +public class BlogDbContext : DbContext +{ + public BlogDbContext(DbContextOptions options) : base(options) + { + } + public DbSet Blogs { get; set; } = null!; +} +``` + +2. 在Program.cs文件中添加连接数据库语句,以及执行操作代码 +```cs +var connectionString = $"Server=.;Database=MdBlog;uid=sa;pwd=123456;TrustServerCertificate=true"; + +builder.Services.AddDbContext(opt => +{ + opt.UseSqlServer(connectionString); +}); +builder.Services.AddScoped(); +``` + +3. 终端相关运行步骤 + - 安装工具“dotnet-ef”:`dotnet tool install --global dotnet-ef` + + - 安装包 Microsoft.EntityFrameworkCore.Design + + `dotnet add package Microsoft.EntityFrameworkCore.Design` + + - 添加迁移数据:`dotnet ef migrations add InitCreate` + + - 将迁移文件更新到数据库:`dotnet ef database update` \ No newline at end of file diff --git "a/\345\210\230\346\254\242/20241211--Linq\347\273\203\344\271\240.md" "b/\345\210\230\346\254\242/20241211--Linq\347\273\203\344\271\240.md" new file mode 100644 index 0000000..2c1f30a --- /dev/null +++ "b/\345\210\230\346\254\242/20241211--Linq\347\273\203\344\271\240.md" @@ -0,0 +1,214 @@ +```cs +public class LinqTestController : Controller +{ + // 查询特定元素 找出数组中等于5的元素 + public dynamic One() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num = numbers.Where(x => x == 5); + return num; + } + // 查询特定范围的元素 找出数组中在2到8之间的元素。 + public dynamic Two() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num = numbers.Where(x => x >= 2 && x <= 8); + return num; + } + // 查询并转换元素 将数组中的每个数字乘以2。 + public dynamic Tree() + { + int[] numbers = { 1, 2, 3, 4, 5, 6 }; + var num = numbers.Select(x => x * 2); + return num; + } + // 查询特定属性的对象 找出所有名字以"王"开头的学生。 + public dynamic Fuor() + { + 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 num = students.Where(x => x.Name.StartsWith('王')); + return num; + } + // 查询并排序 找出所有年龄大于20岁的学生,并按年龄降序排列。 + public dynamic Five() + { + 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 num = students.Where(x => x.Age > 20).OrderByDescending(x => x.Age); + return num; + } + // 查询并去重 找出数组中所有不重复的数字。 + public dynamic Six() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Distinct(); + return num; + } + // 查询第一个元素 找出数组中第一个大于3的元素 + public dynamic Seven() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.FirstOrDefault(x => x > 3); + return num.ToString(); + } + // 查询最后一个元素 找出数组中最后一个小于7的元素。 + + public dynamic Eight() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.LastOrDefault(x => x < 7); + return num.ToString(); + } + // 查询元素是否存在 检查数组中是否存在大于10的元素。 + public dynamic Nine() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Any(x => x > 10); + return num; + } + // 查询元素的计数 计算数组中大于5的元素数量。 + public dynamic Ten() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Count(x => x > 5); + return num.ToString(); + } + // 查询元素的总和 计算数组中所有元素的总和。 + public dynamic Eleven() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Sum(); + return num; + } + // 查询元素的最大值 找出数组中的最大值。 + public dynamic Twelve() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Max(); + return num.ToString(); + } + // 查询元素的最小值 找出数组中的最小值。 + public dynamic Thirteen() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Min(); + return num.ToString(); + } + // 查询元素的平均值 计算数组中所有元素的平均值。 + public dynamic Fourteen() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Average(); + return num.ToString(); + } + // 查询特定条件的元素 找出数组中能被3整除的元素。 + public dynamic Fifteen() + { + int[] numbers = { 1, 2, 3, 4, 5, 6, 18, 23, 64, 7, 18, 2, 3 }; + var num = numbers.Where(x => x % 3 == 0); + return num; + } + // 查询并选择特定属性 找出所有学生的姓名和年龄。 + public dynamic T16() + { + 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 num = students.Select(x => new { x.Name, x.Age }); + return num; + } + // 查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + public dynamic T17() + { + 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 num = students.GroupBy(x => x.Age).Select(s => new { Age = s.Key, Count = s.Count() }); + return num; + + } + + // 查询并联结 联结学生和课程表,找出每个学生的所有课程。 + public dynamic T18() + { + 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 list = students.Join(courses, s => s.Id, c => c.StudentId, (students, courses) => new { studentName = students.Name, courseName = courses.CourseName }); + return list; + } + // 查询并反转 反转数组中的元素顺序。 + public dynamic T19() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num = numbers.Reverse(); + return num; + } +} + +public class Student +{ + public int Id { get; set; } + public string Name { get; set; } = null!; + public int Age { get; set; } + +} + +public class Course +{ + public int StudentId { get; set; } + public string CourseName { get; set; } = null!; + +} +``` \ No newline at end of file -- Gitee