简体   繁体   中英

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.

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

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM