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

相关推荐
ytttr8732 小时前
基于 C# WinForm 实现的 电影院售票系统
开发语言·c#
石像鬼₧魂石2 小时前
SET的钓鱼网站钓鱼模块
windows·学习·ubuntu
喵了几个咪3 小时前
开箱即用的 GoWind Admin|风行,企业级前后端一体中后台框架:Makefile 在后端开发中的应用与 Windows 环境配置
windows
qq 180809513 小时前
多智能体编队重构与协同避障方法探索
c#
love530love4 小时前
在 PyCharm 中配置 x64 Native Tools Command Prompt for VS 2022 作为默认终端
ide·人工智能·windows·python·pycharm·prompt·comfyui
gf13211115 小时前
python_制作视频开头_根据短句字长占总字幕的长度比例拆分
windows·python·音视频
非凡ghost5 小时前
eDiary电子日记本(记录生活点滴)
windows·学习·生活·软件需求
纸带5 小时前
如何理解USB 配置描述符wTotalLength位运算深度
linux·网络·windows
love530love5 小时前
Win11+RTX3090 亲测 · ComfyUI Hunyuan3D 全程实录 ③:diso 源码编译实战(CUDA 13.1 零降级)
开发语言·人工智能·windows·python·comfyui·hunyuan3d·diso
l1t5 小时前
利用docker在windows 11 wsl中安装oracle 12cR2
运维·windows·docker·oracle·容器