I'm new using Dapper and I want a better way to fetch data from a certain column in a Dapper row. Data is being retrieved from a stored procedure that returns 1 or more rows with 5 columns but I only need to get 1 column data for this specific method. My code is actually working but I know there's a better way to write this and would love to apply it.
Here's the sample result from stored procedure: {{DapperRow, prodid = 'b1', prodname = 'sample product', description = 'test only', qty = '1', remainingstock = '10'}}
Here's the snippet of my code:
var products = new List<Product>();
using (var db = DbConnection)
{
var data = //sp call here
//code for improvement starts here
foreach (var row in data)
{
foreach (var col in row)
{
if (col.Key == "prodname")
{
var product = new Product
{
DisplayResult = col.Value
};
products.Add(product);
break; //skip the remaining columns
}
}
}
//to here
}
return products;
expected result would be: products[0].DisplayResult = "sample product"