繁体   English   中英

LINQ查询以获取ASP.Net MVC 5应用程序中的计数

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

在编写LINQ查询时需要帮助,以获取基于部门的员工人数。

部门模式

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

员工模式

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查询

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

预期产量

Department                 Count (or employees)

Human Resource               10

Information Tech              5

问题如何编写LINQ查询以获取上述输出。 请指导我。

您有2个用于修改查询的选项。 我假设您的Department模型包含要分配给视图模型的Department属性的属性string Name

  1. 获取分组集合中的第一个Department ,并将其Name属性分配给视图模型Department属性。 请注意,您需要添加.ToList()之前.GroupBy()以便查询的第一部分是分组之前执行

     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. .GroupBy()表达式更改为按Department属性的Name属性分组

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

这里的关键是按DepartmentId和名称分组

像这样尝试:

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

您可以使用以下代码:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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