我正在使用MVC3并仍在学习LINQ。我在尝试将查询转换为LINQ到实体时遇到了一些麻烦。我想返回一个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格式返回数据。
答案 0 :(得分:1)
假设您可以将值作为参数传递给方法:
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);
}
BTW我有意纠正了Employee
答案 1 :(得分:1)
这是你需要的:
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);
}
您确定要完成所有这些&#34; ToList&lt;&gt;()&#34;电话?一旦调用&#34; ToList&lt;&gt;()&#34;,就可以将这三个表从数据库中带入内存。如果它们很大,那可能是性能问题。
答案 2 :(得分:1)
如果这是控制器操作,您可能希望通过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);
}
答案 3 :(得分:0)
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);
}
我有......