From 46c7937c4576b1adfe9b9953adada2b18b14e4a2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9C=B1=E6=95=AC=E9=91=AB=28=E6=B5=B4=E7=9A=87=E5=A4=A7?= =?UTF-8?q?=E5=B8=9D=E7=89=88=29?= <3284280335@qq.com> Date: Sun, 22 Dec 2024 17:04:26 +0800 Subject: [PATCH] =?UTF-8?q?20241222=E7=AC=94=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...da\350\241\250\350\276\276\345\274\217.md" | 2 +- .../20241212-Linq\346\237\245\350\257\242.md" | 15 ++++++ ...5\346\225\260\346\215\256\345\272\2231.md" | 48 +++++++++++++++++++ ...5\346\225\260\346\215\256\345\272\2232.md" | 32 +++++++++++++ ...5\346\225\260\346\215\256\345\272\2233.md" | 31 ++++++++++++ 5 files changed, 127 insertions(+), 1 deletion(-) create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-Linq\346\237\245\350\257\242.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241216-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2231.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241218-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2232.md" create mode 100644 "\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241219-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2233.md" diff --git "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" index d78a713..cbb8816 100644 --- "a/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" +++ "b/\346\234\261\346\225\254\351\221\253/\344\275\234\344\270\232\345\277\253\347\205\247/\344\270\223\351\241\271\347\273\203\344\271\240-Linq\351\233\206\346\210\220\346\237\245\350\257\242\345\222\214Lambda\350\241\250\350\276\276\345\274\217.md" @@ -80,7 +80,7 @@ public class Linqs:Controller return View(a); } - // 7. + } diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-Linq\346\237\245\350\257\242.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-Linq\346\237\245\350\257\242.md" new file mode 100644 index 0000000..fa84ee2 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241212-Linq\346\237\245\350\257\242.md" @@ -0,0 +1,15 @@ + ```C# + 查询并去重 + public ActionResult Index_6() + { + int[] numbers = { 1, 2, 3, 4, 5, 6,18,23,64,7,18,2,3 }; + var a = numbers + // GroupBy(n => n):将数组中的数字按照值进行分组。 + .GroupBy(n => n) + // Where(g => g.Count() == 1):筛选出只出现一次的组。 + .Where(g => g.Count() == 1) + // Select(g => g.Key):选择这些组的键,即只出现一次的数字。 + .Select(g => g.Key); + return View(a); + } + ``` \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241216-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2231.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241216-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2231.md" new file mode 100644 index 0000000..bb3ab72 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241216-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2231.md" @@ -0,0 +1,48 @@ +数据库的连接 +准备工作 +先在models文件夹中创建一个数据库连接文件,名字随意,这里我创建的是BookDbContext +```C# +using Microsoft.EntityFrameworkCore; + +namespace BookManage.Models; + + +public class BookDbContext:DbContext +{ + + + public DbSet books{get;set;}=null!; + + + protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder) + { + base.OnConfiguring(optionsBuilder); + var key=$"Server=localhost;database=BookDb;uid=sa;pwd=123456;TrustServerCertificate=True"; + optionsBuilder.UseSqlServer(key); + + } + +} +然后再创建一个类对象如Book +namespace BookManage.Models; + +public class Book{ + + public int id{set;get;} + public string BookName{get;set;}=null!; + + public string Author{get;set;}=null!; + + +} +然后安装依赖包 +dotnet add package Microsoft.EntityframeworkCore.SqlServer +然后再添加Design依赖包 +dotnet add package Microsoft.EntityframeworkCore.Design +然后再安装工具 +dotnet tool install --global dotnet-ef +然后迁移数据 +dotnet ef migtration add XXX +然后再更新数据库 +dotnet ef database update +``` \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241218-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2232.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241218-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2232.md" new file mode 100644 index 0000000..716ed21 --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241218-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2232.md" @@ -0,0 +1,32 @@ +设置控制台代码 +```C# +using Microsoft.AspNetCore.Mvc; + +namespace BookManage.Models; + +public class BookManageController : Controller +{ + private readonly ILogger _logger; + + public BookManageController(ILogger logger) + { + _logger = logger; + } + + protected internal BookDbContext db=new BookDbContext(); + + public IActionResult Index() + { + + var books=db.books.ToList(); + return View(books); + } + + public IActionResult Privacy() + { + return View(); + } +} +有这个才可以引用数据库 +protected internal BookDbContext db=new BookDbContext(); +``` \ No newline at end of file diff --git "a/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241219-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2233.md" "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241219-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2233.md" new file mode 100644 index 0000000..925d2ce --- /dev/null +++ "b/\346\234\261\346\225\254\351\221\253/\350\257\276\345\240\202\347\254\224\350\256\260/20241219-\345\242\236\345\210\240\346\224\271\346\237\245\351\223\276\346\216\245\346\225\260\346\215\256\345\272\2233.md" @@ -0,0 +1,31 @@ +编辑页面 +```C# +@model Book + +
+
+ + +
+ +
+ + +
+ +
+ + +
+ + + +
+ + +``` \ No newline at end of file -- Gitee