Is there anyway to iterate through a Dapper DynamicParameters object?

Try:

            var sb = new StringBuilder();
            foreach (var name in p.ParameterNames)
            {
                var pValue = p.Get<dynamic>(name);
                sb.AppendFormat("{0}={1}\n", name, pValue.ToString());
            }

    string ParametersToString(DynamicParameters parameters)
    {
        var result = new StringBuilder();

        if (parameters != null)
        {
            var firstParam = true;
            var parametersLookup = (SqlMapper.IParameterLookup)parameters;
            foreach (var paramName in parameters.ParameterNames)
            {
                if (!firstParam)
                {
                    result.Append(", ");
                }
                firstParam = false;

                result.Append('@');
                result.Append(paramName);
                result.Append(" = ");
                try
                {
                    var value = parametersLookup[paramName];// parameters.Get<dynamic>(paramName);
                    result.Append((value != null) ? value.ToString() : "{null}");
                }
                catch
                {
                    result.Append("unknown");
                }
            }

        }
        return result.ToString();
    }

Tags:

Dapper