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

相关推荐
Deng8723473484 小时前
代码语法检查工具
linux·服务器·windows
Halo_tjn5 小时前
虚拟机相关实验概述
java·开发语言·windows·计算机
张人玉6 小时前
技术开发文档:MES 系统与西门子 PLC 通信软件集成方案
https·c#·plc·mes系统·西门子s7
programer_337 小时前
本地手动创建一个MCP(windows环境)
windows·python·ai·mcp·cherry studio
曹牧7 小时前
Java:List<Map<String, String>>转换为字符串
java·开发语言·windows
BIBI20499 小时前
Windows 下 Git 常规操作教程:命令行与 TortoiseGit
windows·git·tortoisegit·配置·版本控制·入门指南
地球驾驶员9 小时前
NX二次开发C#----C#和C++的二次开发程序如何签名?
c#
fushan201210 小时前
Windows 虚拟机配置与驱动安装记录
windows·k8s·vm·kubevirt
一瞬祈望10 小时前
【环境配置】Windows 下使用 Anaconda 创建 Python 3.8 环境 + 安装 PyTorch + CUDA(完整教程)
pytorch·windows·python
小码编匠10 小时前
C# 实现网络文件传输:打造稳定可靠的工业级工具
后端·c#·.net