Unbreakable
Unbreakable

Reputation: 8132

LINQ Query for getting count in ASP.Net MVC 5 Application

Need help in writing LINQ Query to get count of employee based on department.

Department Model

public class Department
{
    public int Id { get; set; }
    public string Name { get; set; }
    public List<Employee> Employees { get; set; }
}

Employee Model

public class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public int DepartmentId { get; set; }
    public Department Department { get; set; }
}

MyViewModel

public class MyViewModel
{
    public string Department { get; set; }
    public int count { get; set; }
}

LINQ Query

    public ActionResult shows()
    {
        //one department can have many employees and one employee can only be parrt of one department
        // below LINQ query fetches the count of employees belonging to each department
        var x = _context.Employees.Include("Department").GroupBy(e => e.DepartmentId)
     .Select(y=> new MyViewModel{
             Department= y.Key, // ERROR HERE AS Cannot type cast string to integer
            count = y.Count()               
        }).ToList();
     // code removed for brevity
        return Content("x");
    }

Output Expected

Department                 Count (or employees)

Human Resource               10

Information Tech              5

Question How to write a LINQ Query to get the output as above. Please guide me.

Upvotes: 0

Views: 6543

Answers (4)

MSH
MSH

Reputation: 193

you can use this code :

using System.Linq;
using System.Web.Mvc;
using MvcApplication1.Models;

namespace MvcApplication1.Controllers
{
     
     public class HomeController : Controller
     {
          public ActionResult Index()
          {
               var dataContext = new MovieDataContext();
               int count = (from row in dataContext.Movies
                    where row.IsEnquiry == true
                 select row).Count();
               ViewBag.ItemCount = count;
               return View();
          }
     }
}

Upvotes: 0

Mihail Shishkov
Mihail Shishkov

Reputation: 15887

public ActionResult shows()
    {
        //one department can have many employees and one employee can only be parrt of one department
        // below LINQ query fetches the count of employees belonging to each department
        var x = _context.Employees.Include("Department").GroupBy(e => new { e.DepartmentId, e.Department.Name})
     .Select(y=> new MyViewModel{
             Department= y.Key.Name
            count = y.Count()               
        }).ToList();
     // code removed for brevity
        return Content("x");
    }

the key here is to group by DepartmentId AND Name

Upvotes: 1

user3559349
user3559349

Reputation:

You have 2 options for modifying your query. I am assuming your Department model contains a property string Name which you want to assign to the Department property of your view model.

  1. Get the first Department in the grouped collection and assign its Name property to your view models Department property. Note that you need to add .ToList() before .GroupBy() so that the first part of the query is executed before grouping

    var x = _context.Employees
        .Include("Department")
        .ToList()
        .GroupBy(e => e.DepartmentId)
        .Select(y => new MyViewModel
        {
             Department = y.First().Department.Name,
             count = y.Count()               
        }).ToList();
    
  2. Change the .GroupBy() expression to group by the Name property of the Department property

    var x = _context.Employees
        .Include("Department")
        .GroupBy(e => e.Department.Name)
        .Select(y=> new MyViewModel
        {
            Department = y.Key,
            count = y.Count()               
        }).ToList();
    

Upvotes: 1

Mihail Stancescu
Mihail Stancescu

Reputation: 4138

Try it like this:

public ActionResult shows()
{
    //one department can have many employees and one employee can only be parrt of one department
    // below LINQ query fetches the count of employees belonging to each department
    var x = _context.Employees.Include("Department").GroupBy(e => e.DepartmentId)
 .Select(y=> new MyViewModel{
         Department= y.Key.DepartmentName, // or whatever property you have for storing the name
        count = y.Count()               
    }).ToList();
 // code removed for brevity
    return Content("x");
}

Upvotes: 0

Related Questions