It will be very important to generate a ViewModel
(which would be a new class representing SQL
). The typed data is simpler to use on a screen with ASP.NET MVC and also ORM Entity Framework .
The Entity Framework quietly generates this
SQL
you just need to use the extension methods correctly ( Where, Join, GroupBy, Count in Select , etc) to generate this same SQL that you brought in your question or else use SQL Raw as that example.
Example:
ViewModel
public class DadosViewModel
{
public int IdCliente { get; set; }
public string NomeCliente { get; set; }
public decimal ValorVenda { get; set; }
public long TotalItems { get; set; }
}
SQL Raw
You can use this in your Context (Entity Framework) variable:
string SQL = " select b.IdCliente, b.NomeCliente, Sum(ValorVenda) as ValorVenda, ";
SQL += " Count(*) as TotalItens from TabVendas a inner join TabCliente b ";
SQL += " on a.IdCliente = b.IdCliente";
IList<DadosViewModel> ListaDadosViewModel = Contexto.Database
.SqlQuery<DadosViewModel>(SQL).ToList();
SQL by the Entity Framework
IList<DadosViewModel> ListaDadosViewModel = Contexto
.Vendas
.GroupBy(c => c.Cliente)
.Select(s => new DadosViewModel {
IdCliente = s.Key.IdCliente,
NomeCliente = s.Key.NomeCliente,
ValorVenda = s.Sum(v => v.ValorVenda),
TotalItems = s.LongCount()
})
.toList();
View
@model IEnumerable<ViewModels.DadosViewModel>
@foreach(ViewModels.DadosViewModel item in Model)
{
}