我正在使用MVC3,但仍在学习LINQ。我在尝试将查询转换为LINQ to Entities时遇到了一些问题。我想返回一个Json方法
我的存储过程
Create Procedure [dbo].[ResourceReports]
(
@EmployeeID int
)
as
begin
select p.projectName AS Projects, count( b.[CreatedByID]) AS Bugs
from [EmployeeDetails] e inner join [Bugs] b on e.[EmployeId] = b.[CreatedByID]
inner join Projects p on b.ProjectId = p.ProjectId
where e.[EmployeId] = @EmployeeID
group by P.projectName
end
我有几个表,我开始在LINQ中写这篇文章,但我不知道如何正确地返回正确的类型或强制转换它。
我的控制器
public JsonResult Getchart()
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId = @EmployeId
group p.projectName
select new (p.projectName as Project ,count(b.CreatedByID) as Bug)).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
我将如何为查询传递参数,希望数据以json格式返回。
假设您可以将值作为参数传递给方法:
public JsonResult Getchart(int employeeId)
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeeDetails
join b in Bug on e.EmployeeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeeId == employeeId // <-- use the parameter here
group p by p.projectName into g
select new {
Project = g.Key,
Bug = g.Count()
}
).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
顺便说一句,我故意更正了Employee
的一些拼写
如果这是一个控制器操作,您可能希望通过URL传递ID。此外,在查询之前,不需要在表上调用ToList
,在数据库中进行查询,只下拉结果,例如
public JsonResult GetChart(int employeeId)
{
var query = (from e in db.EmployeeDetails
join b in db.Bugs on e.EmployeeId equals b.CreatedById
join p in db.Projects on b.ProjectId equals p.ProjectId
where e.EmployeeId == employeeId
group new {p, b} by new {
p.ProjectName
} into g
select new {
Project = g.Key.Name,
Bugs = g.Count()
}).Take(50);
return Json(query.ToList(), JsonRequestBehaviour.AllowGet);
}
这就是您所需要的:
public JsonResult Getchart(int employeId)
{
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var result = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId == employeeId
group p.projectName
select new (p.projectName as Project ,count(b.CreatedByID) as Bug)).Take(50);
return Json(result,JsonRequestBehavior.AllowGet);
}
您确定要执行所有这些"ToList<>()"调用吗?一旦调用"ToList<>()",就可以将这三个表全部从数据库中放入内存。如果它们很大,那可能是性能问题。
public JsonResult GetChart()
{
//int employeeId
var Bug = db.Bugs.ToList<Bug>();
var EmployeDetails = db.EmployeeDetails.ToList<EmployeeDetail>();
var projects = db.Projects.ToList<Project>();
var query = (from e in EmployeDetails
join b in Bug on e.EmployeId equals b.CreatedByID
join p in projects on b.ProjectId equals p.ProjectId
where e.EmployeId == 1
group new { p, b } by new
{
p.projectName
} into g
select new ChartModel
{
ProjectName = g.Key.projectName,
bug = g.Count()
}).ToList();
return Json(query, JsonRequestBehavior.AllowGet);
}
我得到了。。。