Click here to Skip to main content
15,913,587 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public class StudentDetails
{
public string Type{get; set;}
public DateTime StartDate { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public DateTime EndDate { get; set; }
public string Course { get; set; }
}


I have created List using above code and is used it in a following code :
C#
[WebMethod]
public StudentDetails[] GetTodayReport(int StudentID, DateTime startdate)
{
DateTime tartdate = DateTime.UtcNow.AddDays(-3);
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, tartdate) == 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 }
            );
return query.Select(x=>new StudentDetails() { Type = x.Type, StartDate = x.StartDate, Name = x.Name, Status = x.Status , EndDate = x.EndDate, Course = x.Course }).ToArray();
}
catch (Exception ex)
{
return null;
}
}
}

But an error occured at the X.StartDate and x.EndDate as :
Error 23 Cannot implicitly convert type 'System.DateTime?' to 'System.DateTime'. An explicit conversion exists (are you missing a cast?)

Please let me know if solution exists
Posted
Updated 11-Sep-14 23:28pm
v2

your class:
C#
public class StudentDetails
{
public string Type{get; set;}
public DateTime StartDate { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public DateTime EndDate { get; set; }
public string Course { get; set; }
}


Change like below:
C#
public class StudentDetails
{
public string Type{get; set;}
public DateTime? StartDate { get; set; }
public string Name { get; set; }
public string Status { get; set; }
public DateTime? EndDate { get; set; }
public string Course { get; set; }
}
 
Share this answer
 
You can use this example:
C#
DateTime? nullableDate = new DateTime();
DateTime regularDate = nullableDate.HasValue ? nullableDate.Value : default(DateTime);
 
Share this answer
 
Comments
Cool Smith 15-Apr-22 11:32am    
This will throw an exception
Value of '1/1/0001 12:00:00 AM' is not valid for 'Value'. 'Value' should be between 'MinDate' and 'MaxDate'
.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900