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()
};

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: