c# - List<string> 上的 Dapper sql 查询内部连接

标签 c# dapper

我正在尝试运行以下查询但出现错误。

conn.Query("select d.ID, d.CategoryID from Document d inner join @Cases c on d.CaseID = c.ID", new { Cases = new List<string> { "000-6575-101", "5902-205" }});

当我运行命令时,我收到 Incorrect syntax near ','.

我的问题是,是否有可能像我正在做的那样做?

最佳答案

Dapper 支持 in这种情况的语法:

var ids = new List<string> { "000-6575-101", "5902-205" };
conn.Query("select d.ID, d.CategoryID from Document d where d.CaseID in @ids", new { ids});

这是 dapper 实际上会更改您的查询以执行您想要的操作(同时保持完全参数化等)的少数情况之一。

它还支持(可选,参见 SqlMapper.Settings ):

  • 参数填充以减少查询计划缓存饱和度
  • 使用string_split在 SQL 2016 上用于整数类型(List<int> 等)

关于c# - List<string> 上的 Dapper sql 查询内部连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37820446/

相关文章:

c# - 如何在已安装的服务上授予对 HTTP 命名空间的权限?

c# - 如何更改 Dapper 扩展中的方言?

stored-procedures - 在 Dapper 中使用存储过程有什么缺点吗?

c# - 使用 Dapper 从 C# 调用存储过程时遇到问题

c# - 对 LINQ 语句进行逆向工程

c# - 自定义属性 : An attribute argument must be a constant expression

c# - 'if' 语句条件中的 Lambda 表达式

.net - 嵌套多映射的 Dapper 语法

c# - 我可以从 Dapper 查询返回多个派生类型的集合吗

c# - Mono Gtk# 无法编译初学者教程