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

相关推荐
hoaxxcj2 小时前
DeepSeek Harness 本地部署与第三方插件排障实录:从 fetch failed 到 preset not found
人工智能·windows·开源·ai agent·deepseek
鱼听禅2 小时前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
江湖人称菠萝包4 小时前
【Windows】《深入浅出Windows API程序设计:核心编程篇》笔记-Chapter11-PE文件格式深入剖析
windows·笔记
geovindu4 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
普通人64 小时前
win系统docker运行
windows·docker
淡海水5 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota6 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆6 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota6 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota6 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf