【问题标题】:Time date conversion in linqlinq中的时间日期转换
【发布时间】:2008-10-21 17:45:24
【问题描述】:

我想将格式为“7/20/2008”的日期时间与数据库中格式为“7/20/2008 7:14:53 AM”的日期时间进行比较。

我尝试使用“like”子句,但它不起作用,因为“like”子句仅使用字符串,而我使用的是日期时间格式。

谁能告诉如何在数据库中转换和比较它并提取日期时间。

  protected void User_Querytime()
    {
    DataClasses2DataContext dc1 = new DataClasses2DataContext();
    DateTime date1;


    string date = Request.QueryString.Get("TimeOfMessage");
    date1 = Convert.ToDateTime(date);

    var query7 = from u in dc1.syncback_logs
                 where u.TimeOfMessage = date1
                 orderby u.TimeOfMessage descending
                 select u;
    GridView1.DataSource = query7;
    GridView1.DataBind();
    }

【问题讨论】:

    标签: .net linq linq-to-sql


    【解决方案1】:

    我假设您遇到了问题,因为date1 仅包含日期,而您的数据库包含完整的日期/时间值。要查找匹配项,您需要选择以下方法之一:

    1) 从数据库值中删除时间信息,然后再将它们与您的目标进行比较 2) 将您的目标转换为一个范围,然后在该范围内查找数据库值。

    List<DateTime> dateTimes = new List<DateTime>();
    dateTimes.Add(DateTime.Parse("7/20/2008 7:14:53 AM"));
    dateTimes.Add(DateTime.Parse("7/20/2008 12:12:01 AM"));
    dateTimes.Add(DateTime.Parse("7/21/2008 9:00:00 AM"));
    dateTimes.Add(DateTime.Parse("7/20/2009 7:14:53 AM"));
    
    DateTime targetDate = Convert.ToDateTime("7/20/2008");
    
    // Remove time info from data in database
    var matchingDates = from date in dateTimes
                        where date.Date == targetDate
                        select date;
    
    // Or use your target date to create a range
    DateTime rangeStart = new DateTime(targetDate.Year, targetDate.Month, targetDate.Day, 0, 0, 0);
    DateTime rangeEnd = new DateTime(targetDate.Year, targetDate.Month, targetDate.Day, 23, 59, 59);
    
    var matchingDates2 = from date in dateTimes
                         where (date >= rangeStart) && (date <= rangeEnd)
                         select date;
    

    【讨论】:

      【解决方案2】:

      虽然我无法测试您的确切问题,但我能够将日期与以下代码进行比较。

          // Random Date Collection
          List<DateTime> dateTimes = new List<DateTime>();
          dateTimes.Add(DateTime.Parse("7/20/2008 7:14:53 AM"));
          dateTimes.Add(DateTime.Parse("7/20/2008 7:14:54 AM"));
          dateTimes.Add(DateTime.Parse("7/20/2009 7:14:53 AM"));
      
          DateTime myDateTime = DateTime.Parse("7/20/2008");
      
          var query = from d in dateTimes
                      where d.ToShortDateString() == myDateTime.ToShortDateString()
                      select d;
      

      【讨论】:

      • 我认为这行不通(如果可以,它会很慢),因为您要先转换为字符串(慢比较),然后是 ToShortDateString,我怀疑这是否可以转换为 SQL。
      【解决方案3】:

      不要直接比较日期,而是将它与您接受的最小值/最大值进行比较。因此,不要使用“DbDateCol = mydate”,而是使用“DbDateCol >= myDate.Date and DbDateCol

      通过使用 Date 属性,您将去掉时间分量(强制它为 0)。通过添加一天,您将获得下一天的开始,并将其限制在您的 DateTime 实例的日期上。

      【讨论】:

        【解决方案4】:

        假设我正确理解您的问题,您应该可以使用

        where u.TimeOfMessage.Date == date1
        

        【讨论】:

          【解决方案5】:

          假设 TimeOfMessage 属性是 DateTime 那么你应该能够做到 TimeOfMessage.Date == date1

          【讨论】:

            【解决方案6】:
            using System.Data.Objects;
            

            使用上面并修改您的查询,如下所示:

             var bla = (from log in context.Contacts
                           where EntityFunctions.TruncateTime(log.ModifiedDate) < today.Date
                           select log).FirstOrDefault();
            

            【讨论】:

              猜你喜欢
              • 2012-05-13
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2017-09-23
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-12-25
              相关资源
              最近更新 更多