I'm using Dapper to map object properties .
See a practical example:
var servico = conexao.Query<Servico>(statement.ToSql(), new { IdServico = id }).First();
That is, the Servico
object will have all its properties mapped according to the Servico
table 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 see clarified.
Doubt
I would like to know if it is possible to map and get only one property or field of an SQL query using Dapper? And the field to be obtained would be the Descricao
field as follows in the illustration example.
Example illustration illustration:
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
Can not implicitly convert type 'System.Collections.Generic.IEnumerable' to 'string'