GROUP BY Query in LINQ TO ENTITY


Introduction:

When we create our application using LINQ, in that case we have to write all SQL queries using LINQ query operators. And it is a general requirement to write GROUP BY query when we try to build reports in our application in most cases.

CODE:

Following is the code or Query example to write GROUP BY Query in LINQ to Entities or Entity Framework:

var objData = from f in DASEntity.FILES
from fr in DASEntity.FILE_REQUEST_DETAILS
from s in DASEntity.STATUS
where f.ID == fr.FILE_ID
&& fr.STATUS_ID == s.ID
&& f.CLIENT_TOKEN == ClientToken
&& fr.CLIENT_TOKEN == ClientToken
group s by new { s.ID, s.NAME } into t
select new
{
StatusID = t.Key.ID,
Name = t.Key.NAME,
FileCount = t.Count()
};