How do I perform Date Comparison in EF query?

后端 未结 11 1281
悲哀的现实
悲哀的现实 2020-11-30 07:33

Please help. I am trying to figure out how to use DATE or DATETIME for comparison in a linq query.

Example: If I wanted all Employee names for those who started befo

相关标签:
11条回答
  • 2020-11-30 07:39

    I'm curious to the error message saying 'Date', when you're passing a 'DateTime'. Could it be that 'StartDateColumn' is actually a 'Date', rather than a 'DateTime' in the database? That might mess up the comparison...

    0 讨论(0)
  • 2020-11-30 07:40

    It may be due to the date in the database being nullable. Try this:

    var EmployeeName =
    from e in db.employee
    where e.StartDateColumn.Value <= startDT 
    
    0 讨论(0)
  • 2020-11-30 07:44

    You can not use .Date

    If you would like to check for today you can create a datetime with no time

    DateTime myDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
    var e = (from mds in myEntities.Table
             where mds.CreateDateTime >= myDate
             select mds).FirstOrDefault();
    
    0 讨论(0)
  • 2020-11-30 07:44

    try this:

    DateTime dd = DateTime.Parse("08/13/2010 00:00:00");
    var data = from n in ContributionEligibilities
               where n.ModifiedDateTime.Date >= DateTime.Parse("08/13/2010").Date
               select n; 
    data.Dump("Result") ;
    
    0 讨论(0)
  • 2020-11-30 07:52

    .Date did not work, but .Day did for me.

    var query = from o in Payments
        where o.Order.OrderDate.Day != o.PaymentDate.Day
        orderby o.Order.OrderDate
        select new
        {
         o.Order.OrderID,
         o.Order.OrderDate,
         o.PaymentDate,      
         o.Order.FirstName,
         o.Order.LastName,
         o.Order.CustomerID
        };
    
    
    query.Dump();
    
    0 讨论(0)
  • 2020-11-30 07:53

    That should work. Are you sure that there isn't another part of the query that triggered the exception? I have several instances of queries of the form

    var query = from e in db.MyTable
                where e.AsOfDate <= DateTime.Now.Date
                select e;
    

    in my code.

    0 讨论(0)
提交回复
热议问题