c# sqlsugar 批量查询

一.功能实现

在C#中使用SqlSugar进行批量查询,可以使用Queryable对象的In方法来执行批量查询。以下是一个使用SqlSugar进行批量查询的示例代码:

cs 复制代码
using SqlSugar;
 
// 假设有一个实体类Person
public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
    // 其他属性...
}
 
// 进行批量查询的方法
public List<Person> BatchQuery(SqlSugarClient db, IEnumerable<int> ids)
{
    return db.Queryable<Person>().In(it => it.Id, ids).ToList();
}
 
// 使用示例
SqlSugarClient db = new SqlSugarClient(new ConnectionConfig()
{
    ConnectionString = "your_connection_string",
    DbType = DbType.SqlServer,
    IsAutoCloseConnection = true,
    InitKeyType = InitKeyType.Attribute
});
 
// 假设我们有一个ID集合
List<int> idList = new List<int> { 1, 2, 3, 4, 5 };
 
// 执行批量查询
List<Person> people = BatchQuery(db, idList);

二.总结

上述代码示例摘自百度AI回答,其中有两点值得点出

1.查询方法中In的使用

cs 复制代码
db.Queryable<Person>().In(it => it.Id, ids).ToList()

2.IEnumerable的使用

cs 复制代码
IEnumerable<int> ids

公开枚举器,该枚举器支持对指定类型的集合进行简单迭代(微软官方文档),这个用法有点泛型编程的意思,对比c++的模版和c#的泛型,是值得研究的地方

相关推荐
河阿里34 分钟前
Windows批处理脚本(.bat)从零到精通:详细教程与实战案例
windows
idolao36 分钟前
Android Studio 2022安装与汉化教程 Windows版:解压+管理员运行+自定义路径+SDK配置+中文插件指南
android·windows·android studio
摇滚侠1 小时前
Groovy 如何给集合中添加元素
java·开发语言·windows·python
~plus~1 小时前
C# 事件溯源与 CQRS 架构:用 EventStoreDB 打造可靠系统
开发语言·架构·c#
aq55356001 小时前
Laravel7.x重磅升级:十大新特性解析
开发语言·汇编·c#·html
wei_shuo1 小时前
解放双手!用Windows搭建闲鱼0成本“赚米神器”!AI客服秒回复!
人工智能·windows
洛水如云2 小时前
换新电脑数据迁移全攻略:2 种方法转移文件 / 系统
windows·microsoft·电脑
chushiyunen3 小时前
posting替换postman(好像还是不太好用)
windows
overmind3 小时前
oeasy Python 120[专业选修]列表_直接赋值_浅拷贝_shallowcopy_深拷贝_deepcopy
linux·windows·python
唐青枫3 小时前
C#.NET Mapperly 深入解析:源生成映射、安装使用与工程化取舍
c#·.net