[英]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
。
獲取分組集合中的第一個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();
將.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.