流浪のwolf

卷帝

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

统计

C# efcode 新建表格数据 增删改查

复制代码
using TestDbContext ctx = new TestDbContext();
var b1 = new Book
{
    AuthorName = "杨中科",
    Title = "零基础趣学C语言",
    Price = 59.8,
    PubTime = new DateTime()
};
var b2 = new Book
{
    AuthorName = "Robert Sedgewick",
    Title = "算法(第4版)",
    Price = 99,
    PubTime = new DateTime()
};
var b3 = new Book
{
    AuthorName = "吴军",
    Title = "数学之美",
    Price = 69,
    PubTime = new DateTime()
};
var b4 = new Book
{
    AuthorName = "杨中科",
    Title = "程序员的SQL金典",
    Price = 52,
    PubTime = new DateTime()
};
var b5 = new Book
{
    AuthorName = "吴军",
    Title = "文明之光",
    Price = 246,
    PubTime = new DateTime()
};
ctx.Books.Add(b1);
ctx.Books.Add(b2);
ctx.Books.Add(b3);
ctx.Books.Add(b4);
ctx.Books.Add(b5);
await ctx.SaveChangesAsync();

Console.WriteLine("-==============测试==============");
Console.WriteLine(long.MaxValue);
复制代码

查询数据库

using TestDbContext ctx = new TestDbContext();
foreach (Book b in ctx.Books)
{
    Console.WriteLine($"Id={b.Id},Title={b.Title},Price={b.Price}");
}

Console.WriteLine("-==============测试==============");
Console.WriteLine(long.MaxValue);

条件查询

Console.WriteLine("***所有价格高于80元的书籍***");
IEnumerable<Book> books2 = ctx.Books.Where(b => b.Price > 80);
foreach (Book b in books2)
{
    Console.WriteLine($"Id={b.Id},Title={b.Title},Price={b.Price}");
}
复制代码
//数据的插入
/*
using TestDbContext ctx = new TestDbContext();
var b1 = new Book{ AuthorName = "杨中科", Title = "零基础趣学C语言", 
    Price = 59.8, PubTime = new DateTime(2019, 3, 1) };
var b2 = new Book{ AuthorName = "Robert Sedgewick", Title = "算法(第4版)", 
    Price = 99, PubTime = new DateTime(2012, 10, 1) };
var b3 = new Book{ AuthorName = "吴军", Title = "数学之美", 
    Price = 69, PubTime = new DateTime(2020, 5, 1) };
var b4 = new Book{ AuthorName = "杨中科", Title = "程序员的SQL金典", 
    Price = 52, PubTime = new DateTime(2008, 9, 1) };
var b5 = new Book{ AuthorName = "吴军", Title = "文明之光", 
    Price = 246, PubTime = new DateTime(2017, 3, 1) };
ctx.Books.Add(b1);
ctx.Books.Add(b2);
ctx.Books.Add(b3);
ctx.Books.Add(b4);
ctx.Books.Add(b5);
await ctx.SaveChangesAsync();*/
//查询
/*
using TestDbContext ctx = new TestDbContext();
Console.WriteLine("***所有书籍***");
foreach (Book b in ctx.Books)
{
    Console.WriteLine($"Id={b.Id},Title={b.Title},Price={b.Price}");
}
Console.WriteLine("***所有价格高于80元的书籍***");
IEnumerable<Book> books2 = ctx.Books.Where(b => b.Price > 80);
foreach (Book b in books2)
{
    Console.WriteLine($"Id={b.Id},Title={b.Title},Price={b.Price}");
}*/
//Single、FirstOrDefault查询
/*
using TestDbContext ctx = new TestDbContext();
Book b1 = ctx.Books.Single(b => b.Title == "零基础趣学C语言");
Console.WriteLine($"Id={b1.Id},Title={b1.Title},Price={b1.Price}");
Book? b2 = ctx.Books.FirstOrDefault(b => b.Id == 9);
if (b2 == null)
{
    Console.WriteLine("没有Id=9的数据");
}
else
{
    Console.WriteLine($"Id={b2.Id},Title={b2.Title},Price={b2.Price}");
}*/
//排序
/*
using TestDbContext ctx = new TestDbContext();
IEnumerable<Book> books = ctx.Books.OrderByDescending(b => b.Price);
foreach (Book b in books)
{
    Console.WriteLine($"Id={b.Id},Title={b.Title},Price={b.Price}");
}*/
//GroupBy

using TestDbContext ctx = new TestDbContext();
var groups = ctx.Books.GroupBy(b => b.AuthorName)
    .Select(g => new { AuthorName = g.Key, BooksCount = g.Count(), MaxPrice = g.Max(b => b.Price) });
foreach (var g in groups)
{
    Console.WriteLine($"作者:{g.AuthorName},图书数量:{g.BooksCount},最高价格:{g.MaxPrice}");
}
//修改数据
/*
using TestDbContext ctx = new TestDbContext();
var b = ctx.Books.Single(b => b.Title == "数学之美");
b.AuthorName = "Jun Wu";
await ctx.SaveChangesAsync();
*/
//删除数据
/*
using TestDbContext ctx = new TestDbContext();
var b = ctx.Books.Single(b => b.Title == "数学之美");
ctx.Remove(b);//也可以写成ctx.Books.Remove(b);
await ctx.SaveChangesAsync();*/
/*
using TestDbContext ctx = new TestDbContext();
Book b = new Book
{
    AuthorName = "Zack Yang",
    Title = "Zack, Cool guy!",
    Price = 9.9,
    PubTime = new DateTime(2020, 12, 30)
};
ctx.Books.Add(b);
Console.WriteLine($"保存前,Id={b.Id}");
await ctx.SaveChangesAsync();
Console.WriteLine($"保存后,Id={b.Id}");*/
复制代码

 

posted on   朱龙旭的网络  阅读(59)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· Blazor Hybrid适配到HarmonyOS系统
· 支付宝 IoT 设备入门宝典(下)设备经营篇
· 万字调研——AI生成内容检测
· 解决跨域问题的这6种方案,真香!
· 一套基于 Material Design 规范实现的 Blazor 和 Razor 通用组件库
点击右上角即可分享
微信分享提示