您好我在sql server中进行了一个查询,如下所示,sp名称为 sp_StudentRequest
Select DISTINCT SR.StudentRequestId,SR.RegistrationId,SR.Location,SR.PaymentMethod,SR.CreatedOn,C.ClassName,CC.CampusName,
CASE WHEN ISNULL(TSR.StatusId,0)=0 THEN 1 ELSE TSR.StatusId END AS StatusId,
substring(
(
Select ', '+REPLACE(REPLACE(ST1.FromTime,'AM',''),'PM','')+'-'+ST1.ToTime AS [text()]
From dbo.StudentRequestTimings ST1
Where ST1.StudentRequestId = SRT.StudentRequestId
ORDER BY ST1.CreatedOn
For XML PATH ('')
), 2, 1000) [Time]
FROM StudentRequest SR
Inner JOIN Registration R ON R.RegistrationId=SR.RegistrationId
INNER JOIN Campus CC ON CC.CampusId=R.CampusId
INNER JOIN Class C ON C.ClassId=SR.ClassId
LEFT JOIN TutorClasses TC ON SR.ClassId=TC.ClassId
LEFT JOIN StudentRequestTimings SRT ON SR.StudentRequestId=SRT.StudentRequestId
LEFT JOIN TutorStudentRequest TSR ON TSR.StudentRequestId=SRT.StudentRequestId AND TutorId=@RegistrationId
where TC.RegistrationId=@RegistrationId
ORDER BY SR.CreatedOn DESC
我将这个代码从控制器调用为
public ActionResult TutorDashboard(int? page)
{
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("sp_StudentRequest"))
{
DataSet GetData = new DataSet();
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("@Operation", "GetTutorRequestDetail");
cmd.Parameters.AddWithValue("@RegistrationId", 1);
cmd.CommandType = CommandType.StoredProcedure;
SqlDataAdapter ad = new SqlDataAdapter(cmd);
ad.Fill(GetData);
if (GetData.Tables[0].Rows.Count > 0)
{
ViewBag.TutorRequest = GetData.Tables[0].ToPagedList(page ?? 1, 1);
}
con.Close();
}
}
return View(ViewBag.TutorRequest);
}
现在我需要在PagedList的帮助下在MVC中实现分页,我该怎么办?
如果我无法使用此sql查询,那么在实体框架中转换此查询的方法是什么?
任何帮助对我都有用。
答案 0 :(得分:0)
要应用分页,我已在实体框架工作中转换了我的查询,下面是代码
var query = (from sr in db.StudentRequests
join r in db.Registrations on sr.RegistrationId equals r.RegistrationId
join cc in db.Campus on r.CampusId equals cc.CampusId
join c in db.Classes on sr.ClassId equals c.ClassId
from tc in db.TutorClasses.Where(t => t.ClassId == sr.ClassId).DefaultIfEmpty()
from srt in db.StudentRequestTimings.Where(s => s.StudentRequestId == sr.StudentRequestId).DefaultIfEmpty()
from tsr in db.TutorStudentRequests.Where(t => t.StudentRequestId == srt.StudentRequestId && t.TutorId == registrationid).DefaultIfEmpty()
where tc.RegistrationId == registrationid
select new
{
StudentRequestId = sr.StudentRequestId,
RegistrationId = sr.RegistrationId,
Location = sr.Location,
PaymentMethod = sr.PaymentMethod,
CreatedOn = sr.CreatedOn,
ClassName = c.ClassName,
CampusName = cc.CampusName,
StatusId = tsr.StatusId == null ? 1 : tsr.StatusId,
Time = db.StudentRequestTimings.Where(p => p.StudentRequestId == sr.StudentRequestId).Select(p => p.FromTime.ToString().Replace("AM", "").Replace("PM", "") + "-" + p.ToTime)
}).ToList().GroupBy(p => new { p.StudentRequestId }).Select(g => g.First()).ToList();
var model = query.AsEnumerable().Select(x => new TutorDashboard
{
StudentRequestId = x.StudentRequestId,
RegistrationId = x.RegistrationId,
Location = x.Location,
PaymentMethod = x.PaymentMethod,
CreatedOn = x.CreatedOn,
ClassName = x.ClassName,
CampusName = x.CampusName,
StatusId = x.StatusId == null ? 1 : x.StatusId,
Time = string.Join(",", x.Time),
}).ToList().ToPagedList(page ?? 1, 1);
最后我在观看方面传递了这个模型。