Dapper可以处理动态列标题

时间:2014-06-19 20:12:04

标签: dapper

我有一个总是返回字符串列表的存储过程。但是,根据传递给存储过程的参数,字符串列表的列标题将更改。 Dapper能够处理这个吗?如果是这样,应该如何处理?

                conn.Open();
                var p = new DynamicParameters();
                p.Add("Search_Function_CD", searchFunction, DbType.String, ParameterDirection.Input);
                p.Add("Search_Value", searchValue, DbType.String, direction: ParameterDirection.Input);
                p.Add("o_resultset", dbType: DbType.Object, direction: ParameterDirection.Output);
                var Results = (IDictionary<string, object>)conn.Query(sql: CommonConstants.ProcedureConstants.PKG_GET_SEARCH_RESULTS, param: p, commandType: CommandType.StoredProcedure);
                foreach (object o in Results)
                {
                    string element = Results.Values.ElementAt(1) as string;
                    searchResults.Add(element);
                }
                conn.Close();
                return searchResults;

1 个答案:

答案 0 :(得分:5)

您可以通过动态列名称或索引获取值:

var row = (IDictionary<string, object>)con.Query("select * from Products").First();

string name = row["ProductName"]; // column name 'ProductName'
// or:
string name = row.Values.ElementAt(1) as string; // column index 1