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

相关推荐
wrq14721 小时前
MyAccess 完整使用手册
c#·orm框架
美丽的欣情1 天前
RK3588 Debian 交叉编译环境搭建(Windows + VMware Debian + CMake)
运维·windows·debian
勤劳打代码1 天前
Flutter 架构指南 —— windows 平台安装拆解
windows·架构
z落落1 天前
C# WinForm 自定义控件
开发语言·c#
lzhdim1 天前
20260714 - 个人小作品更新
c#·notenet
aramae1 天前
C++11:现代C++的里程碑
c语言·开发语言·c++·windows·git·后端
执行x1 天前
wsl2安装+桥接模式+固定IP
linux·windows·ubuntu
做萤石二次开发的哈哈1 天前
如何接入萤石开放平台 ERTC Windows SDK?
windows·音视频·萤石开放平台
橙臣程1 天前
在 Windows 中使用 WSL2 搭建 Ubuntu 开发环境
linux·windows·ubuntu
小堂子这厢有礼了1 天前
Chet.QuartzNet.UI v2.3.0 大更新!表格重构 + 系统配置 + 分析页全面升级!
前端·后端·ui·重构·c#·vue