【问题标题】:In LINQ Query convert the string to datetime and check with today's date在 LINQ 查询中将字符串转换为日期时间并检查今天的日期
【发布时间】:2014-01-10 10:04:22
【问题描述】:

在 Table 中,DueDate 数据类型为 varchar。现在我想用今天的日期检查到期日

 var query = (from o in db.Order_Reports
              where Convert.ToDateTime(o.ReportDueDateTime) >= DateTime.Now
              select o);

错误

LINQ to Entities 无法识别方法 'System.DateTime ToDateTime(System.String)' 方法,并且该方法无法转换为存储表达式。

我想如何将字符串转换为日期和时间并检查今天的日期

【问题讨论】:

  • 它不会像你描述的那样工作......但是可能有一种解决方法。 ReportDueDateTime 列的确切格式是什么?是 YYYYMMDD 吗?
  • 你为什么又问这个问题?你接受的答案有什么你不喜欢的地方吗?为什么DueDatevarchar 肯定是要解决的问题?
  • @DarkWanderer - ReportDueDateTime 格式可能是 MM/dd/yyyy 或 M/d/yyyy

标签: c# linq entity-framework datetime


【解决方案1】:

我不知道如何实现它,因为

  1. SQL 中没有函数可以通过不等式过滤字符串格式的日期(至少那些适用于您的日期格式并且可以从 LINQ 转换为 SQL 查询的函数)。
  2. 您的数据库设计存在缺陷,导致“正常”方法无法正常工作

但是,有一些可能的解决方法

  1. 修改数据库,使日期为实际的日期时间格式(最好的选择,但当然有时不可用)
  2. 创建一个存储过程来实现相同的目标。在SQL代码中,可以使用日期解析
  3. 将查询修改如下:

(代码)

// Our required date
DateTime reportDate = new DateTime(2014,1,1).Date; 
// Let's find number of days between now and required day. Ensure that the date is not in the future!
int deltaDays = (DateTime.Now.Date - date).Days; 
// Let's get the list of dates which we need the reports for
var dates = Enumerable.Range(0, deltaDays + 1).Select(dd => DateTime.Now.Date.AddDays(-dd).ToString("MM/dd/yyyy")).ToArray();
// and query by this list
var query = (from o in db.Order_Reports
          where o.ReportDueDateTime in dates
          select o);

这样效率会有点低,但是不改变DB就达到了目的。将其视为临时解决方案。

【讨论】:

    【解决方案2】:

    试试 DateTime.Parse(o.ReportDueDateTime) 看看是否可行

    【讨论】:

    • 不工作错误 - LINQ to Entities 无法识别方法 'System.DateTime Parse(System.String)' 方法,并且此方法无法转换为存储表达式。
    • 在这种情况下,在 Linq 查询之外声明一个字符串变量并将其设置为 DateTime.Now.ToString() 然后在 Linq 查询中执行字符串比较,有点散列,但它应该可以工作。但是,如果可能,您应该考虑将数据库列更改为 DateTime。
    • 公平点 - 我没有注意到这一点。这个问题确实源于数据库的原始设计,列应该是您存储的实际类型,而不是存储为 varchar。
    【解决方案3】:

    问题在于 Linq-to-Entities 无法将 Convert.ToDateTime 转换为有效的 sql。如果您通过调用AsEnumerable()ToList() 将所有记录带入内存,那么您将使用Linq-To-Sql,这将与C# 函数一起使用:

    var query = (from o in db.Order_Reports.AsEnumerable()
                  where DateTime.ParseExact(o.ReportDueDateTime, 
                                            "MM/dd/yyyy", 
                                            "M/d/yyyy") >= DateTime.Now
                  select o);
    

    这样做的问题是您可能会将太多记录带入内存。但是,如果您有足够的报告让您担心,那么您可能应该在 ReportDueDateTime 上放置一个数据库索引,并且您需要该索引按日期顺序排列。所以将该列更改为DateTime 或将数据更改为可以按字母顺序排序的格式 - 例如“YYYY-MM-DD-HH-mm”。然后你可以这样做:

    var query = (from o in db.Order_Reports
                 where String.Compare(ReportDueDateTime, 
                                      DateTime.Now.ToString("YYYY-MM-DD-HH-mm")) >= 0
                  select o);
    

    【讨论】:

    • 显示错误,比如 operator '>' cannot be applied to 'string' and 'string' 类型的操作数
    • 改用String.Compare(o.ReportDueDateTime, DateTime.Now .ToString("YYYY-MM-DD-HH-mm") >= 0
    • 不确定您尝试了哪一个,但刚刚意识到Convert.ToDateTime might not work if the culture doesn't match. Edited to use ParseExact
    【解决方案4】:

    是的,这是 Linq-to-Entities 中的一个问题 除了nullable 之外,这种情况发生在常规 DateTime 时

    Linq 可以理解Convert.ToDateTimeGetValueOrDefault()

    但 Entitie 没有这样做

    你可以使用 Date.Value
    这段代码:

    var x = myContext.MyTable.Where(tbl => tbl .MyDateColumn.Value == DateTime.Now)
    

    【讨论】:

      【解决方案5】:

      你可以使用 String.Compare。

      var query = from o in db.Order_Reports
                  where String.Compare(o.ReportDueDateTime, Now.ToString()) > 0
                  select o;
      

      【讨论】:

        【解决方案6】:

        也许这会起作用

        // Make new variable in DateTime Format
        DateTime dateFrom = DateTime.ParseExact(o.ReportDueDateTime, "MM/dd/yyyy", CultureInfo.InvariantCulture);
        
        var query = (from o in db.Order_Reports
                      where dateFrom >= DateTime.Now
                      select o);
        

        【讨论】:

        • o.ReportDueDateTime 不能在 LINQ 语句之外引用。
        • 我的错,对不起。我使用动态 LINQ 库来完成类似的任务。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-01
        • 1970-01-01
        • 2018-07-16
        • 1970-01-01
        • 1970-01-01
        • 2011-09-26
        • 1970-01-01
        相关资源
        最近更新 更多