Can Dapper handle dynamic column header

dapper

Question

I have a stored procedure that always returns a list of strings. However, based on the parameters passed to the stored procedure, the column heading for the list of strings will change. Is Dapper able to handle this? If so, how should it be handled?

                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;

Popular Answer

You can get the value by a dynamic column name or index:

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



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why