如何在Dapper删除查询中使用IN子句作为GUID的列表

问题描述 投票:-1回答:1

如何在删除dapper查询中使用in子句?

DELETE FROM [tableName] where [columnName] in ({list of guids});

我尝试了下面的代码,但出现“唯一标识符转换”错误:

//EmpIds is List of Guids;
var empIds = string.Format("{0}", string.Join(",", EmpIds.Select(x => $"'{x}'").ToArray()));

using (var connection = new SqlConnection(ConnectionString))
{
    connection.Open();
    var deletedRowsCount = connection.Execute("
                         DELETE FROM [Employee] WHERE EmployeeId in                         
                       (@empIds)",  new { empIds = empIds });
                        //EmployeeId is uniqueidentifier datatype in sqlserver db
}

任何人都告诉我如何在dapper查询中解决此问题以进行批量删除。

c# sql-server dapper
1个回答
0
投票

dapper中List<>支持的语法与实际t-sql查询中的语法略有不同。请参阅:https://github.com/StackExchange/Dapper

DELETE FROM [Employee] 
WHERE EmployeeId in @empIds

[IN子句的参数不需要大括号。而且您只需传递IEnumerable<>。像这样new { empIds = new[] { Guid.NewGuid(), Guid.NewGuid() } }

© www.soinside.com 2019 - 2024. All rights reserved.