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

相关推荐
love530love28 分钟前
冷门干货!llama.cpp 自带原生网页聊天 UI,无需第三方依赖一键开启
人工智能·windows·ui·llama·flash-attention·switch-cuda
gordon~91 小时前
Windows 11 wsl 中安装的Ubuntu-24.04 中装docker
windows·ubuntu·docker
历程里程碑3 小时前
Protobuf 环境搭建:Windows 与 Linux 系统安装教程
linux·运维·数据结构·windows·线性代数·算法·矩阵
tobebetter95273 小时前
WSL2 + Windows + remote Chrome CDP openclaw 浏览器自动化
chrome·windows·自动化
qq_454245033 小时前
GraphFoundation动态更新图
架构·c#·图论
愤豆4 小时前
07-Java语言核心-JVM原理-JVM对象模型详解
java·jvm·c#
桌面运维家4 小时前
解决Windows 10打印机脱机:端口、驱动、网络故障排除
windows·stm32·单片机
张人玉4 小时前
上位机项目笔记
笔记·c#·上位机
liwulin05065 小时前
【ROS2】【ESP32S3纯透传方案】ESP32S3+WINDOWS+VMware+ROS2+YDLIDA X导航完整落地方案
windows·stm32·单片机
AxureMost5 小时前
MTools 0.0.12-beta 全能工具箱
windows·开源软件