how to find grouping data and counter in linq

Description:
Here i will explain group by count multiple columns in linq. It is easy and short ways to find group by data with counter in linq like the below simple example to do it:

var result = (from emp in db.Employees
              where emp.companyID.Equals(companyid)
              group emp by emp.departmentID into g
              select new
              {
                deptid = g.Key,
                countemp = g.Count()
              }).ToList();

In above query we get the employees of selected companyid and than group by department wise and select that column that you want to display in the list.

No comments:

Post a Comment