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#的泛型,是值得研究的地方

相关推荐
曹牧30 分钟前
C#:线程间操作无效,不是从创建控件线程访问
开发语言·c#
毕竟是shy哥35 分钟前
windows电脑WSL下本地构建docker镜像
windows·docker·容器
anlog9 小时前
ini文件读取,EXE同目录文件读取
c#·ini
rockey6279 小时前
AScript之编译递归函数
c#·.net·script·动态脚本
kakakahahahaha10 小时前
【Windows】C盘低空间反复复发的排查与扩容边界
c语言·windows·电脑·笔记本电脑·内容运营·软件需求
2301_8000742112 小时前
map,list简单方法
windows·python·list
技术不好的崎鸣同学12 小时前
Windows 红队实战:前言
windows
吴声子夜歌13 小时前
Guava——基本工具(二)
windows·python·guava
猎嘤一号14 小时前
【2026 最新】Windows 11 右键菜单还原为 Windows 10 经典样式:一条命令、原理、回退与新版说明
windows·python
qq_3692243315 小时前
ddraw.dll丢失是什么原因引起的?老游戏运行报错,多种可行修复手段汇总分享
windows·游戏·dll·dll修复·dll丢失·dll错误