我正在使用 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 中编写它,但我不确定如何正确返回正确的类型或强制转换它。
我的 Controller
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
的一些拼写
关于c# - 将存储过程转换为 LINQ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12092399/