From a5f9fbef157ae7f2ede7d72398b82072d0b25db7 Mon Sep 17 00:00:00 2001 From: Jon Hynes Date: Tue, 30 Jan 2024 05:49:09 -0500 Subject: [PATCH] Fix: Slowness when using large number of dynamic parameters #1537 --- Dapper/DynamicParameters.cs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Dapper/DynamicParameters.cs b/Dapper/DynamicParameters.cs index f6708b5ce..92bd48298 100644 --- a/Dapper/DynamicParameters.cs +++ b/Dapper/DynamicParameters.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Data; +using System.Globalization; using System.Linq; using System.Linq.Expressions; using System.Reflection; @@ -222,6 +223,9 @@ protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) } } + HashSet addedParameters = new HashSet(command.Parameters.Cast() + .Select(x => x.ParameterName), comparer: StringComparer.CurrentCultureIgnoreCase); + foreach (var param in parameters.Values) { if (param.CameFromTemplate) continue; @@ -250,7 +254,7 @@ protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) } else { - bool add = !command.Parameters.Contains(name); + bool add = !addedParameters.Contains(name); IDbDataParameter p; if (add) { @@ -296,6 +300,8 @@ protected void AddParameters(IDbCommand command, SqlMapper.Identity identity) } param.AttachedParam = p; } + + addedParameters.Add(name); } // note: most non-privileged implementations would use: this.ReplaceLiterals(command);