From bc7be9e69dfbe8d98fb3bad153af81eb499f4222 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=AE=B8=E5=A4=9A=E9=92=B1?= <3381810463@qq.com> Date: Sun, 15 Dec 2024 20:36:29 +0800 Subject: [PATCH] =?UTF-8?q?20241209=E7=AC=94=E8=AE=B010=E3=80=8120241211?= =?UTF-8?q?=E7=AC=94=E8=AE=B011=E3=80=8120241213=E7=AC=94=E8=AE=B012?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\346\225\260\346\215\256\345\272\223.md" | 77 +++++++++++++++++++ ...da\350\241\250\350\276\276\345\274\217.md" | 67 ++++++++++++++++ ...33\351\230\266\350\256\262\350\247\243.md" | 49 ++++++++++++ 3 files changed, 193 insertions(+) create mode 100644 "\350\256\270\350\211\263/20241209\347\254\224\350\256\26010--\344\273\243\347\240\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md" create mode 100644 "\350\256\270\350\211\263/20241211\347\254\224\350\256\26011--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217.md" create mode 100644 "\350\256\270\350\211\263/20241213\347\254\224\350\256\26012--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217\347\273\203\344\271\240\350\277\233\351\230\266\350\256\262\350\247\243.md" diff --git "a/\350\256\270\350\211\263/20241209\347\254\224\350\256\26010--\344\273\243\347\240\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md" "b/\350\256\270\350\211\263/20241209\347\254\224\350\256\26010--\344\273\243\347\240\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md" new file mode 100644 index 0000000..39d887d --- /dev/null +++ "b/\350\256\270\350\211\263/20241209\347\254\224\350\256\26010--\344\273\243\347\240\201\345\210\233\345\273\272\346\225\260\346\215\256\345\272\223.md" @@ -0,0 +1,77 @@ +## 概念 +### 常用数据库 +1. Sqlserver 微软 +2. PostgreSQL 开源产品 社区驱动 +3. Mysql/MariaDb 开源产品 公司驱动 +4. Oracle 商业产品 +5. Sysbase 商业产品 +6. DB2 商业产品 +7. 达梦 +8. 人大金仓 +9. Redis 非关系型数据库 开源 +10. Memarchem 非关系型数据库 开源 +11. MongoDb 非关系型数据库 开源 +### 常用的ORM工具(应用和数据库打交道的工具) +1. Dapper 开源产品 特点:速度快,但是代码写起来很麻烦 +2. EntityFrameworkCore 微软 特点:代码写起来很方便,但是速度慢 + 1. 数据库优先(以前流行) + 2. 代码优先(现在较流行) + 1. 定义数据库模型 + 2. 生成迁移文件(执行一个命令) +3. FreeSql 开源 +4. Hibernet 开源 + +## 代码创建数据库 +1. 定义数据库模型 +```cs +// 母 +public class Student +{ + public int ID{get;set;} + public string Name{get;set;}=null!; + public int Age{get;set;} +} + +// 子 +namespace Blog.Models; +using Microsoft.EntityFrameworkCore; + +public class StuDb : DbContext +{ + public StuDb(DbContextOptions options) : base(options) + { + + } + + + public DbSet Students { get; set; } = null!; + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + } +} +``` +2. 在program中添加的数据库语句 +```cs +var connectionString=$".\\SQLEXPRESS;Database=MyDb;uid=sa;pwd=123456;TrustServerCertificate=true"; + +builder.Services.AddDbContext(opt => +{ + opt.UseSqlServer(connectionString); +}); + +builder.Services.AddScoped(); + +``` +3. 终端运行代码 +``` + +dotnet add package Microsoft.EntityFrameworkCore.Sqlserver + +dotnet tool install --global dotnet-ef + +dotnet ef migrations add InitCrate + +dotnet ef database update +``` \ No newline at end of file diff --git "a/\350\256\270\350\211\263/20241211\347\254\224\350\256\26011--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217.md" "b/\350\256\270\350\211\263/20241211\347\254\224\350\256\26011--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217.md" new file mode 100644 index 0000000..8574e7a --- /dev/null +++ "b/\350\256\270\350\211\263/20241211\347\254\224\350\256\26011--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217.md" @@ -0,0 +1,67 @@ +```cs + // 16.找出所有学生的姓名和年龄。 + public dynamic Six1(){ + 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; + } + // 17.查询并分组 按年龄分组学生,并计算每个年龄组的学生数量。 + public dynamic Seven1(){ + 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; + } + + // 18.查询并联结 联结学生和课程表,找出每个学生的所有课程。 + public dynamic Eight1(){ + 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 num = students.Join(courses, s => s.Id, c => c.StudentId,(students, courses) => new { studentName = students.Name, courses = courses.CourseName }); + return num; + } + // 19.查询并反转 反转数组中的元素顺序 + public dynamic Nine1(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num =numbers.Reverse(); + return num; + } +``` \ No newline at end of file diff --git "a/\350\256\270\350\211\263/20241213\347\254\224\350\256\26012--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217\347\273\203\344\271\240\350\277\233\351\230\266\350\256\262\350\247\243.md" "b/\350\256\270\350\211\263/20241213\347\254\224\350\256\26012--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217\347\273\203\344\271\240\350\277\233\351\230\266\350\256\262\350\247\243.md" new file mode 100644 index 0000000..4578933 --- /dev/null +++ "b/\350\256\270\350\211\263/20241213\347\254\224\350\256\26012--Linq\346\237\245\350\257\242\345\222\214lambda\350\241\250\350\276\276\345\274\217\347\273\203\344\271\240\350\277\233\351\230\266\350\256\262\350\247\243.md" @@ -0,0 +1,49 @@ +### 找出数组中第一个大于2的元素,并用它填充后面所有的元素 +```cs +public dynamic Ten1(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num =numbers.First(x=> x>2); + return num; + } +``` +### 查询并使用Zip合并 合并两个数组,并创建一个包含元素对的新数组。 +```cs + public dynamic Five2(){ + int[] numbers1 = { 1, 2, 3 }; + int[] numbers2 = { 4, 5, 6 }; + var num=numbers1.Zip(numbers2,(x,y)=> x + " " + y); + return num; + } +``` +### 生成一个包含1到10的整数数组 +```cs + public dynamic Six2(){ + + int[] numbers = Enumerable.Range(1, 10).ToArray(); + return numbers; + } +``` +### 查询并使用Take限制数量 从数组中取出前5个元素。 +```cs + public dynamic Eight2(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num=numbers.Take(5); + return num; + } +``` +### 重复一个元素多次,创建一个新数组 前面是元素,后面是数量 +```cs + public dynamic Seven2(){ + + int[] numbers = Enumerable.Repeat(8, 5).ToArray(); + return numbers; + } + ``` +### 查询并使用Skip跳过元素 跳过数组中的前3个元素,然后取出剩余的元素。 +```cs + public dynamic Nine2(){ + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var num=numbers.Skip(3); + return num; + } +``` \ No newline at end of file -- Gitee