【发布时间】:2021-02-28 17:31:29
【问题描述】:
我有一个包含数据行的表,其中一些 DateAndTime 数据不正确,当我检索数据时,我得到以下异常 Unable to convert MySQL date/time value to System.DateTime。很难编辑所有记录,但我想做的是跳过不正确的 DateAndTime 格式我该怎么做? 不正确的 DateAndTime 格式可能如下: 0000-00-00 00:00:00 我的模型如下:
public class Orders
{
[Key]
public int Id { get; set; }
public string CreatedBy { get; set; }
public DateTime CreatedDateTime { get; set; }
public int? OrderNumber{ get; set; }
}
我的 LINQ 查询如下:
result = appContext.DatabaseContext.Orders.Where(a => a.Id != -1).ToList();
【问题讨论】:
-
您可能无法跳过这些记录,但您可能想要做的是将日期读取为字符串,然后遍历您获得的内容并将这些记录复制到 DateTime成功进入最终集合。
-
您可以通过在连接字符串中添加
convert zero datetime=True来跳过0000-00-00值。它可以帮助您将这些值转换为DateTime.MinValue,之后您可以简单地使用 LINQ
标签: c# asp.net-mvc linq