I have basic concepts of linq, but I stuck with a query with group by someone could help me translate this query from sql to linq:
select at1,at2,count(1)
from tabla
where fecha=trunc(sysdate)
group by at1,at2
Greetings.
I have basic concepts of linq, but I stuck with a query with group by someone could help me translate this query from sql to linq:
select at1,at2,count(1)
from tabla
where fecha=trunc(sysdate)
group by at1,at2
Greetings.
To express GROUP BY
of multiple columns and COUNT
with LINQ:
query syntax
from entidad in entidades
group entidad by new { entidad.Propiedad1, entidad.Propiedad2 } into grupo
select new { grupo.Key.Propiedad1, grupo.Key.Propiedad2, Count = grupo.Count() };
or lambda syntax
entidades
.GroupBy(entidad => new { entidad.Propiedad1, entidad.Propiedad2 })
.Select(grupo => new { grupo.Key.Propiedad1, grupo.Key.Propiedad2, Total = grupo.Count() });
Apart from what Equiso has answered in a very correct way, you must also bear in mind that this fecha=trunc(sysdate)
, in linq2Entities, is expressed through some static methods of the DbFunctions class or EntitiyFunctions , within the namespace System.Data.Entity , the first is for version 6 of EF and the second for the previous ones. That would be the thing:
using System.Data.Entitiy;
from entidad in entidades
where DbFunctions(entidad.fecha) == DbFunctions(DateTime.Today)
group entidad by new { entidad.Propiedad1, entidad.Propiedad2 } into grupo
select new { grupo.Key.Propiedad1, grupo.Key.Propiedad2, Count = grupo.Count() };