In LINQ Query convert the string to datetime and check with today's date

后端 未结 6 1440
情歌与酒
情歌与酒 2021-01-24 17:24

In Table, DueDate data type is varchar. Now I want to check the due date with today\'s date

 var query = (from o in db.Order_Reports
              where Convert.         


        
相关标签:
6条回答
  • 2021-01-24 17:55

    Maybe this will work

    // 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);
    
    0 讨论(0)
  • 2021-01-24 17:57

    yes this a problem in Linq-to-Entities this case happend when regular DateTime except that it is nullable

    Linq can understand Convert.ToDateTime or GetValueOrDefault()

    But Entitie Not sported that

    you cane use Date.Value
    this Code :

    var x = myContext.MyTable.Where(tbl => tbl .MyDateColumn.Value == DateTime.Now)
    
    0 讨论(0)
  • 2021-01-24 17:58

    The problem is that Linq-to-Entities is not capable of turning Convert.ToDateTime into valid sql. If you bring all the records into memory with a call to AsEnumerable() or ToList() then you will be using Linq-To-Sql and that will work with C# functions:

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

    The problem with that is that you might be bringing too many records into memory. But if you have enough reports for that to be a worry, then you should probably be putting a database index on that ReportDueDateTime and you need that index to be in date order. So change that column to a DateTime or change the data into a format that can be sorted alphabetically - like "YYYY-MM-DD-HH-mm". Then you could do this:

    var query = (from o in db.Order_Reports
                 where String.Compare(ReportDueDateTime, 
                                      DateTime.Now.ToString("YYYY-MM-DD-HH-mm")) >= 0
                  select o);
    
    0 讨论(0)
  • 2021-01-24 18:04

    Try DateTime.Parse(o.ReportDueDateTime) and see if that works

    0 讨论(0)
  • 2021-01-24 18:10

    I don't know a way to implement it as you describe it, because

    1. There's no function in SQL that will filter the dates in string format by inequality (at least those that work with your date format and are convertable from LINQ to SQL query).
    2. You have a flaw in your database design which prevents "normal" methods from working

    However, there are some possible workarounds

    1. Amend the database so that the date is of actual datetime format (best option, but of course sometimes unavailable)
    2. Create a stored procedure to achieve the same goal. In the SQL code, you can use date parsing
    3. Amend the query as follows:

    (code)

    // 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);
    

    This will be a little inefficient, but achieve the purpose without changing the DB. Treat it as temporary solution.

    0 讨论(0)
  • 2021-01-24 18:17

    You could use String.Compare.

    var query = from o in db.Order_Reports
                where String.Compare(o.ReportDueDateTime, Now.ToString()) > 0
                select o;
    
    0 讨论(0)
提交回复
热议问题