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

相关推荐
rockey6275 小时前
C#脚本引擎之AScript与Flee对比
c#·.net·script·eval·动态脚本
Mr.亮先生7 小时前
# 把 DeepSeek 官方 Agent 工作台,直接装进 Windows 启动器里
windows·deepseek·harness
zjnlswd10 小时前
C#学习笔记4
笔记·学习·c#
曹牧11 小时前
C#:List<string>特定格式输出
windows·c#
Z59981784113 小时前
c#软件开发学习笔记--Modbus-TCP/UDP网口通讯
笔记·学习·c#
易点云徐源13 小时前
(已解决)Photoshop 多画板 Alt+拖动复制随机卡死 4~5 分钟
windows·photoshop
Dovis(誓平步青云)14 小时前
DevEco Studio 6.1.1 Windows 安装实录:从下载校验到首次启动
android·开发语言·数据库·人工智能·windows·harmonyos
bosins14 小时前
将 Hyper-V 上的 Ubuntu 虚拟机内容迁移到 WSL (Windows Subsystem for Linux) 中
linux·windows·ubuntu
曹牧14 小时前
C#:取的多个药品批准文号中的一个
c#
ι:15 小时前
DeepSeek Harness 桌面一键启动与网页背景修改指南
windows·学习·deepseekharness