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

相关推荐
波波00712 小时前
写出稳定C#系统的关键:不可变性思想解析
开发语言·c#·wpf
willhuo13 小时前
基于Playwright的抖音网页自动化浏览器项目使用指南
爬虫·c#·.netcore·webview
光泽雨13 小时前
c#文件结构
c#
十五年专注C++开发14 小时前
cpolar(极点云): 一款主流的内网穿透工具
linux·windows·cpolar·穿透
非凡ghost14 小时前
AIMP(音乐播放软件)
前端·windows·音视频·firefox
xifangge202514 小时前
【2026终极解法】彻底解决“由于找不到 msvcp140.dll,无法继续执行代码”报错(附微软运行库一键修复包)
windows·mysql·microsoft·报错·开发环境
摇滚侠14 小时前
Windows 查看占用端口的进程,并关闭进程
windows
汤姆yu14 小时前
Windows系统下TRAE的安装与完整使用教程
windows·ai·trae·ai编程工具
一个人旅程~14 小时前
在M系列的macbook上如何使用VMware安装ARM版的Win11以及注意哪些问题?
linux·windows·经验分享·macos·电脑
love530love14 小时前
Windows 下 vcpkg 依赖环境部署与全局化配置
人工智能·windows·vcpkg