How to return a List<dynamic>
in ASP.NET MVC 5 and EntityFramework 6
For example I have an Object named User and mapped in my EDMX, when I do to bring the data dynamically type make an advanced query without returning an object of type User.
Note: this is a View the vwCase
select top 5 SUM(vrLancamento),nmTipoDocumento from vwCaixa group by nmTipoDocumento
I did so:
public IList<dynamic> resumoCaixa()
{
IList<dynamic> lista = new List<dynamic>();
TimeSpan time = new TimeSpan(DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second);
using (var ctx = new dbProClinicEntities())
{
var t = ctx.vwCaixa.GroupBy(g => new { documento = g.nmTipoDocumento, pago = g.dtPago, valor = g.vrLancamento })
.Select(x => new { pago = x.Key.pago, tipoDocumento = x.Key.documento, vrLancamento = x.Sum(f => f.vrLancamento) })
.Take(5).ToList();
foreach(var f in t)
{
lista.Add(f);
}
}
return lista.ToList();
}
In the Controller I'm returning a ViewBag:
public ActionResult Index()
{
AdminDAO dao = new AdminDAO();
ViewBag.Caixa caixa = dao.resumoCaixa();
return View();
}
In the View I'm doing this:
<table class="table list">
<tbody>
@foreach(var caixa in ViewBag.Caixa)
{
<tr>
<td>
<a href="#"><p class="title">@caixa.vrLancamento</p></a>
<p class="info">@caixa.tipoDocumento</p>
</td>
</tr>
}
</tbody>
</table>
But it gives the following error: