我怎么能隐式转换类型System.DateTime?到System.DateTime [英] How can I implicitly convert type System.DateTime? to System.DateTime

查看:104
本文介绍了我怎么能隐式转换类型System.DateTime?到System.DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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; }
}





我使用上面的代码创建了List,并在以下代码中使用它:



I have created List using above code and is used it in a following code :

[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;
}
}
}



但是X.StartDate和x.EndDate发生了错误:

错误23无法将类型'System.DateTime?'隐式转换为'System.DateTime'。存在显式转换(您是否错过了演员?)



如果存在解决方案,请告诉我

推荐答案

你可以使用这个例子:

You can use this example:
DateTime? nullableDate = new DateTime();
DateTime regularDate = nullableDate.HasValue ? nullableDate.Value : default(DateTime);


你的班级:

your class:
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:

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; }
}


这篇关于我怎么能隐式转换类型System.DateTime?到System.DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆