可以像参数字典一样枚举Dapper DynamicParameters对象吗?

时间:2015-09-24 00:20:38

标签: c# .net-4.5 dapper

我知道我可以使用ParameterDirection与Dapper.DynamicParameters:

var parameters = new DynamicParameters();
parameters.Add("iparam", 42);
parameters.Add("oparam", null, DbType.Int32, ParameterDirection.Output);
connection.Execute(sql, parameters);

但是在使用Dictionary<string, object>时可以这样做吗?

var parameters = new Dictionary<string, object>();
parameters.Add("iparam", 42);
parameters.Add("oparam", /* ??? */ );
connection.Execute(sql, parameters);

或者,如何迭代DynamicParameters以获取参数名称和值?

原标题:

  

可以使用a添加带有ParameterDirection的Dapper参数   字典?

2 个答案:

答案 0 :(得分:9)

这似乎是Dapper中的一个错误。这证实适用于最新的NuGet包:

foreach (var paramName in parameters.ParameterNames)
{
    var value = ((SqlMapper.IParameterLookup)parameters)[paramName];
}

然而,它有点冗长。在本地使用Dapper源(不是NuGet包),我能够毫无错误地运行此代码(截至撰写提交b77e53时):

foreach (var paramName in parameters.ParameterNames)
{
    var value = parameters.Get<dynamic>(paramName);
}

根据Charles Burns' comment,它仍然会抛出异常,这让我相信补丁还没有进入NuGet。修复Get<dynamic>的提交是here

答案 1 :(得分:3)

DynamicParameters对象每次附加时都可以包含多个部分。因此,如上面的答案所示,迭代ParameterNames(它将是空的)并非如此。

public static Dictionary<string, object> ToParametersDictionary(this DynamicParameters dynamicParams)
{
    var argsDictionary = new Dictionary<String, Object>();
    var iLookup = (SqlMapper.IParameterLookup) dynamicParams;
    //if (dynamicParams.ParameterNames.Any())
    //{
        // read the parameters added via dynamicParams.Add("NAME", value)
        foreach (var paramName in dynamicParams.ParameterNames)
        {
            var value = iLookup[paramName];
            argsDictionary.Add(paramName, value);
        }
    //}
    //else
    //{
        // read the "templates" field containing dynamic parameters section added
        // via dynamicParams.Add(new {PARAM_1 = value1, PARAM_2 = value2});
        var templates = dynamicParams.GetType().GetField("templates", BindingFlags.NonPublic | BindingFlags.Instance);
        if (templates != null)
        {
            var list = templates.GetValue(dynamicParams) as List<Object>;
            if (list != null)
            {
                // add properties of each dynamic parameters section
                foreach (var objProps in list.Select(obj => obj.GetPropertyValuePairs().ToList()))
                {
                    objProps.ForEach(p => argsDictionary.Add(p.Key, p.Value));
                }
            }
        }
    }
    return argsDictionary;
}

GetPropertyValuePairs(..)

public static Dictionary<string, object> GetPropertyValuePairs(this object obj, String[] hidden = null)
{
    var type = obj.GetType();
    var pairs = hidden == null
        ? type.GetProperties()
            .DistinctBy(propertyInfo => propertyInfo.Name)
            .ToDictionary(
                propertyInfo => propertyInfo.Name,
                propertyInfo => propertyInfo.GetValue(obj, null))
        : type.GetProperties()
            .Where(it => !hidden.Contains(it.Name))
            .DistinctBy(propertyInfo => propertyInfo.Name)
            .ToDictionary(
                propertyInfo => propertyInfo.Name,
                propertyInfo => propertyInfo.GetValue(obj, null));
    return pairs;
}

public static IEnumerable<TSource> DistinctBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
{
    var seenKeys = new HashSet<TKey>();
    return source.Where(element => seenKeys.Add(keySelector(element)));
}