我可以从Dapper DynamicParameters中删除参数吗?

时间:2019-06-26 20:55:39

标签: c# json dapper

我正在从外部来源检索数据。我的课程与JSON的响应相匹配。但是,json内部有一个内部对象。将其传递给Dapper中的动态参数时如何删除它?

课程的基本结构

{
   "name": "John",
   ... 30 more fields here ...

   "location": {
       "city": "abcd", 
       "zip": "87123"
    }
}

像这样使用

foreach (var result in response.results) 
{
    var parameters = new DynamicParameters();
    parameters.AddDynamicParams(result);

    // I need to remove "location" from this parameter
    // and then I can add the city and zip

    parameters.AddDynamicParams(result.location.city); // etc

    db.Execute("save_data", parameters, commandType:CommandType.StoredProcedure);
}

1 个答案:

答案 0 :(得分:0)

仍在等待答案,但这是我已开始使用的解决方法

foreach (var result in response.results) {
  var parameters = new DynamicParameters();

  foreach (PropertyInfo prop in result.GetType().GetProperties()) {

      if (!SpecialParameter(result, prop, parameters))
         parameters.Add(prop.Name, prop.GetValue(result, null));

  }

  db.Execute("save_data",
         parameters, commandType: CommandType.StoredProcedure);
}    


function SpecialParameter(result, prop, parameters) {
// can be implemented in the sub class

   switch (prop.Name) {
      case "location":
         parameters.Add("location_city", result.city);
         parameters.Add("location_state", result.city);
         return true;
   }

  return false;
}
相关问题