[英]can't convert a string to DateTime
I have problem converting a string to DateTime,this is the error 我在将字符串转换为DateTime时遇到问题,这是错误
I have used the solution showed in this link enter link description here and using DateTime.Parse,but I get this error all the time 我已经使用了此链接中显示的解决方案,请在此处输入链接说明并使用DateTime.Parse,但是我一直都收到此错误
this is my class Schedule: 这是我的课程表:
public partial class Schedule : BaseEntity
{
public string Day { get; set; }
public string Note { get; set; }
public DateTime Begin { get; set; }
public DateTime End { get; set; }
}
any help please and thanks Update:this is teh query 任何帮助请,谢谢更新:这是查询
var Schedules = new List<Schedule>
{
new Schedule
{
Day="Lundi", Note="note", Begin=Convert.ToDateTime("2013-04-24 17:47:03"),
End=Convert.ToDateTime("2013-05-24 17:47:03"),
ClassId = context.Classes.Where(c=>c.Libel=="Class 1").FirstOrDefault().Id,
SubjectLevelId = context.SubjectLevels.Where(cbv=>cbv.Coef==1).FirstOrDefault().Id,
ClassRoomId = context.ClassRooms.Where(c=>c.Libel=="ClassRoom1").FirstOrDefault().Id,
TeacherId = context.Teachers.Where(c=>c.FirstName=="mouna").FirstOrDefault().Id,
SchoolYearId= context.SchoolYears.Where(f=>f.Begin==DateTime.Parse("15/09/2015")).FirstOrDefault().Id
},
};
Schedules.ForEach(s =>
{
s.ObjectState = Repository.Pattern.Infrastructure.ObjectState.Added;
context.Schedules.Add(s);
context.SaveChanges();
});
Change your code to something like this: 将您的代码更改为如下所示:
var beginDateTime = Convert.ToDateTime("2013-04-24 17:47:03");
var endDateTime = Convert.ToDateTime("2013-05-24 17:47:03");
var schoolYearBeginDate = DateTime.Parse("15/09/2015");
var Schedules = new List<Schedule>
{
new Schedule
{
Day="Lundi",
Note="note",
Begin = beginDateTime,
End=endDateTime,
ClassId = context.Classes.Where(c=>c.Libel=="Class 1").FirstOrDefault().Id,
SubjectLevelId = context.SubjectLevels.Where(cbv=>cbv.Coef==1).FirstOrDefault().Id,
ClassRoomId = context.ClassRooms.Where(c=>c.Libel=="ClassRoom1").FirstOrDefault().Id,
TeacherId = context.Teachers.Where(c=>c.FirstName=="mouna").FirstOrDefault().Id,
SchoolYearId= context.SchoolYears.Where(f=>f.Begin==schoolYearBeginDate).FirstOrDefault().Id
},
};
Also, these will eventually throw a null reference exception: 而且,这些最终将引发空引用异常:
context.Classes.Where(c=>c.Libel=="Class 1").FirstOrDefault().Id
Change them to: 将它们更改为:
context.Classes.Where(c=>c.Libel=="Class 1").Select(c => c.Id).FirstOrDefault()
If ClassId
etc are not nullable, then you should do this: 如果
ClassId
等不可为空,那么您应该这样做:
context.Classes.Where(c=>c.Libel=="Class 1").First().Id
尝试在LINQ查询和DateTime.ParseExact
方法中使用AsEnumerable
Begin = DateTime.ParseExact("2013-04-24 17:47:03", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
ParseExact
will serve your purpose if you have specific date provided to this function, like shown below. 如果您为此功能提供了特定的日期,
ParseExact
将满足您的目的,如下所示。 And here is MSDN documentation about this method. 这是有关此方法的MSDN文档。
Begin = DateTime.ParseExact("2013-04-24 17:47:03", "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture);
However ParseExact
fails if you are providing date from some incoming text box or from other control, and date is absent there then it will throw a runtime error. 但是,如果要从某个传入的文本框或其他控件提供日期,则
ParseExact
失败,并且那里不存在日期,则它将引发运行时错误。 To avoid that, best is to use TryParseExact
. 为了避免这种情况,最好是使用
TryParseExact
。 Read here from MSDN about TryParseExact. 阅读在这里从MSDN约TryParseExact。
Datetime dateTime;
bool isValid = DateTime.TryParseExact("2013-04-24 17:47:03", "yyyy-MM-dd HH:mm:ssy", CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime);
isValid
will tell you if conversion was successful and dateTime
will have your new converted date from string. isValid
会告诉您转换是否成功, dateTime
将从字符串中获取新的转换日期。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.