如何将查询中的项目添加到List< StudentDetails> ? [英] How can I add items in a query to the List<StudentDetails> ?
问题描述
我已经提到以下链接:
http://www.aspdotnet-suresh.com/2012/03/bind-data-to-gridview-with-jquery-or.html [< a href =http://www.aspdotnet-suresh.com/2012/03/bind-data-to-gridview-with-jquery-or.html\"target =_ blanktitle =New Window> ^ ]
我在.asmx文件中编写了以下代码,并在.aspx文件中调用此WebMethod
使用 JSON:
[WebMethod]
public StudentDetails [] GetTodayReport( int StudentID,DateTime startdate)
{
try
{
context = new TMSEntities();
列表< studentdetails> details = new 列表< studentdetails>();
List< string> sdetails = new List< string>();
DataTable NewTable = new DataTable();
NewTable.Columns.AddRange( new DataColumn [ 6 ] { new DataColumn( 输入, typeof ( string )), new DataColumn( StartDate, typeof ( string )), new DataColumn( 名称, typeof ( string )), new DataColumn( 状态, typeof ( string )), n ew DataColumn( EndDate, typeof ( string )), new DataColumn( 课程, typeof ( string ))});
var query =( from E in context.tbl_ExamCreation
join EA context.tbl_ExamAttendence on E.Exam_ID等于EA .Exam_ID
join C context.tbl_Course on E.Course_ID等于C.CourseID
其中 EA.StudentID == StudentID&& SqlFunctions.DateDiff( DAY,E.DateOfExam,startdate)== 0 && E.Status ==
选择 new {Typ e = 考试,StartDate = E.DateOfExam,Name = E.ExamName,Status = EA。 status,EndDate = E.ExamEndDatetime,Course = C.Name})
.Union
( from LS in context.tbl_LectureScheduling
join LA in context.tbl_LectureAttendence on LS .LectureScheduleID等于LA.LectureScheduleID
join L 在 context.tbl_Lectures on LS.LectureID等于L .LectureID
join CR context.tbl_Course on L.CourseID等于CR.CourseID
其中 LA.StudentID == StudentID&& SqlFunctions.DateDiff( DAY,LS.StartDateTime,startdate)== 0 && LS.Status == A
选择 new {Type = Lecture ,StartDate = LS.StartDateTime,Name = L.LectureName,Status = LA.status,EndDate = LS.EndDateTime,Course = CR.Name}
);
foreach ( var item in 查询)
{
// 这里我想将上面的查询结果添加到详细信息中list< studentdetails>类型的列表
}
}
catch (例外情况)
{
return null ;
}
}
}
public class StudentDetails
{
public string 输入{获得跨度>; set ;}
public string StartDate { get ; set ; }
public string 名称{ get 跨度>; set ; }
public string 状态{ get 跨度>; set ; }
public string EndDate { get 跨度>; set ; }
public string 课程{ get 跨度>; set ; }
}
}
我想将上面的查询结果添加到详细信息列表中,该列表的类型为List< studentdetails>在上面循环。那我怎么能这样做?
用这个替换你的查询部分
列表与LT; studentdetails> query =( from E in context.tbl_ExamCreation
加入 EA context.tbl_ExamAttendence on E.Exam_ID等于EA.Exam_ID
join C context.tbl_Course on E.Course_ID等于C.CourseID
其中 EA。 StudentID == StudentID&& SqlFunctions.DateDiff( DAY,E.DateOfExam,startdate) == 0 && E.Status ==
选择 new {Type = 考试,StartDate = E.DateOfExam,Name = E.ExamName,Status = EA.status,EndDate = E.ExamEndDatetime,Course = C.Name})
.Union
(来自 LS in context.tbl_LectureScheduling
加入 LA 在 context.tbl_LectureAttendence on LS.LectureScheduleID等于LA.LectureScheduleID
join L context.tbl_Lectures on LS.LectureID equals L.LectureID
join CR context.tbl_Course上的class =code-keyword> L.CourseID等于CR.CourseID
其中 LA.StudentID == StudentID& ;&安培; SqlFunctions.DateDiff( DAY,LS.StartDateTime,startdate)== 0 && LS.Status == A
选择 new StudentDetails(){Type = 讲座,StartDate = LS.StartDateTime,Name = L.LectureName,Status = LA.status,EndDate = LS.EndDateTime,Course = CR.Name}
).ToList();
而不是foreach使用
return query.Select(x => new StudentDetails(){Type = x.Type,StartDate = x.StartDate.HasValue?x.StartDate.Value:DateTime.MinValue,...} ).ToArray();
I have referred following link :
http://www.aspdotnet-suresh.com/2012/03/bind-data-to-gridview-with-jquery-or.html[^]
I have written following code in .asmx file and calling this WebMethod in .aspx file
using JSON:
[WebMethod]
public StudentDetails[] GetTodayReport(int StudentID, DateTime startdate)
{
try
{
context = new TMSEntities();
List<studentdetails> details = new List<studentdetails>();
List<string> sdetails = new List<string>();
DataTable NewTable = new DataTable();
NewTable.Columns.AddRange(new DataColumn[6] { new DataColumn("Type", typeof(string)), new DataColumn("StartDate", typeof(string)), new DataColumn("Name", typeof(string)), new DataColumn("Status", typeof(string)), new DataColumn("EndDate", typeof(string)), new DataColumn("Course", typeof(string)) });
var query = (from E in context.tbl_ExamCreation
join EA in context.tbl_ExamAttendence on E.Exam_ID equals EA.Exam_ID
join C in context.tbl_Course on E.Course_ID equals C.CourseID
where EA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", E.DateOfExam, startdate) == 0 && E.Status == "A"
select new { Type = "Exam", StartDate = E.DateOfExam, Name = E.ExamName, Status = EA.status, EndDate = E.ExamEndDatetime, Course = C.Name })
.Union
(from LS in context.tbl_LectureScheduling
join LA in context.tbl_LectureAttendence on LS.LectureScheduleID equals LA.LectureScheduleID
join L in context.tbl_Lectures on LS.LectureID equals L.LectureID
join CR in context.tbl_Course on L.CourseID equals CR.CourseID
where LA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", LS.StartDateTime, startdate) == 0 && LS.Status == "A"
select new { Type = "Lecture", StartDate = LS.StartDateTime, Name = L.LectureName, Status = LA.status, EndDate = LS.EndDateTime, Course = CR.Name }
);
foreach(var item in query)
{
// Here I want to add above query result into "details" list which is of type List<studentdetails>
}
}
catch (Exception ex)
{
return null;
}
}
}
public class StudentDetails
{
public string Type{get; set;}
public string StartDate { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public string EndDate { get; set; }
public string Course { get; set; }
}
}
I want to add above query result into "details" list which is of type List<studentdetails> in above for loop. So how can I do the same?
Replace your query porion with this
List<studentdetails> query = (from E in context.tbl_ExamCreation join EA in context.tbl_ExamAttendence on E.Exam_ID equals EA.Exam_ID join C in context.tbl_Course on E.Course_ID equals C.CourseID where EA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", E.DateOfExam, startdate) == 0 && E.Status == "A" select new { Type = "Exam", StartDate = E.DateOfExam, Name = E.ExamName, Status = EA.status, EndDate = E.ExamEndDatetime, Course = C.Name }) .Union (from LS in context.tbl_LectureScheduling join LA in context.tbl_LectureAttendence on LS.LectureScheduleID equals LA.LectureScheduleID join L in context.tbl_Lectures on LS.LectureID equals L.LectureID join CR in context.tbl_Course on L.CourseID equals CR.CourseID where LA.StudentID == StudentID && SqlFunctions.DateDiff("DAY", LS.StartDateTime, startdate) == 0 && LS.Status == "A" select new StudentDetails(){ Type = "Lecture", StartDate = LS.StartDateTime, Name = L.LectureName, Status = LA.status, EndDate = LS.EndDateTime, Course = CR.Name } ).ToList();
instead of foreach use
return query.Select(x=>new StudentDetails() { Type = x.Type, StartDate = x.StartDate.HasValue ? x.StartDate.Value : DateTime.MinValue,...}).ToArray();
这篇关于如何将查询中的项目添加到List< StudentDetails> ?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!