通过 uniqueidentifier 选择后使用 dapper 更新多条记录

cpo*_*ign 3 c# dapper

我有许多从数据库加载的记录,我想在返回它们之前更新设置的值。唯一的主要要求是我不希望多个命令一一更新每条记录。

请注意我的 ID 是 GUID(唯一标识符)

到目前为止我的代码是

public IEnumerable<Person> GetUnprocessedPeople(int batchSize)
    {
        List<Queue_ImportQueue> list;
        using (IDbConnection db = OpenedConnection)
        {
            string peopleList = $"SELECT TOP({batchSize}) * FROM [dbo].[Person]";

            list = db.Query<Person>(peopleList).ToList();
            using (IDbTransaction transactionScope = db.BeginTransaction(IsolationLevel.Serializable))
            {
                string updateQuery = $"UPDATE [dbo].[Person] SET Created = GETDATE() WHERE Id ='@ids'";

                try
                {
                    db.Execute(updateQuery, new { Id = list.Select(x => x.Id) }, transactionScope);
                    transactionScope.Commit();
                }
                catch (Exception ex)
                {
                    transactionScope.Rollback();
                    throw;
                }
            }
        }
        return list;
    }
Run Code Online (Sandbox Code Playgroud)

cpo*_*ign 6

好的,解决方案非常简单。不需要特定的铸造。只需确保所有参数正确即可。所以摘录已经为我解决了查询:

            string updateQuery = $"UPDATE [dbo].[Person] SET Created = GETDATE() WHERE Id ='@Ids'";

           ...

            db.Execute(updateQuery, new { Ids = list.Select(x => x.Id) }, transactionScope);
Run Code Online (Sandbox Code Playgroud)