4
I’m using the Dapper that has the purpose of map properties of objects.
See a practical example:
var servico = conexao.Query<Servico>(statement.ToSql(), new { IdServico = id }).First();
That is, the object Servico
will have all its properties mapped according to the table Servico
of the database and the data will be populated in the object at runtime.
However, I have a question regarding property mapping, which I would like to be clarified.
Doubt
I wonder if it is possible to map and get only one property or field from an SQL query using Dapper? And the field to be obtained would be the field Descricao
as follows in the illustration example.
Illustration example:
using (conexao = new SQLiteConnection(StringDeConexao))
{
conexao.Open();
var sql = "select s.Descricao from Servico s where s.Descricao = @Descricao";
string campoDescricao = conexao.Query(sql, new { Descricao = "Descricao de teste" });
}
The above attempt results in the following error:
Error CS0029
Cannot implicitly Convert type 'System.Collections.Generic.Ienumerable' to 'string'
Gabe banned me from chat. I have no way to respond there. Please add me on Facebook.
– Guilherme Nascimento