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

相关推荐
一心赚狗粮的宇叔2 小时前
中级软件开发工程师2025年度总结
java·大数据·oracle·c#
cplmlm3 小时前
EF Core使用CodeFirst生成postgresql数据库表名以及字段名用蛇形命名法,而类名仍使用驼峰命名
c#
吴声子夜歌4 小时前
Java数据结构与算法——基本数学问题
java·开发语言·windows
lingxiao168885 小时前
WebApi详解+Unity注入--下篇:Unity注入
unity·c#·wpf
这儿有一堆花7 小时前
用原生脚本编写无害恶作剧
windows
因我你好久不见7 小时前
Windows部署springboot jar支持开机自启动
windows·spring boot·jar
夜流冰7 小时前
Excel - MS Support for Excel: 2 Collaborate
数据库·windows·excel
lingxiao168888 小时前
WebApi详解+Unity注入--中篇:.net core的WebAPI
unity·c#·.netcore
林瞅瞅8 小时前
PowerShell 启动卡顿?内存飙升?原来是 800MB 的历史记录在作祟!
windows
ServBay8 小时前
C# 成为 2025 年的编程语言,7个C#技巧助力开发效率
后端·c#·.net