3
I’m trying to return dynamic values of a query also dynamic, but I can’t figure out how to get any kind of data from the database.
Here’s what I’m trying to do
comandoSql = @" SELECT " + NomeCampo + " FROM " + Tabela + " WHERE Id = @p0";
var result = _db.Database.SqlQuery<string>(comandoSql, id).ToList();
It works when my return is a varchar field. However as the query is dynamic, I may want to query other fields of other tables, so if a Numeric, datetime etc comes back an error.
I have tried using Sqlquery and Sqlquery but cannot get the value from inside the object after.
Have you tried using Dapper? It will do exactly what you want and is much lighter than EF.
– Victor Laio
with Dapper I can, but for this solution I can’t use Dapper for some internal issues.
– Guilherme Camarotto
Why not try using the EF context? You call the context class of the database and create objects that give access to the table. This would solve?
– Cedric S.
Use the
dynamic
doesn’t solve?_db.Database.SqlQuery<dynamic>(comandoSql, id)
; Then I’d just have to process the information he returns.– João Martins
so I tried using Dynamic, but I can’t handle the information when it returns. The information value is like {Object} only.
– Guilherme Camarotto
But of that
object
can’t get the guy out?object.GetType()
?– João Martins
I can’t do anything with that object. it has no function or property after it returns.
– Guilherme Camarotto
Solved by Dapper even using Dynamic return.
– Guilherme Camarotto