locked
Appointments not being brought back correct. RRS feed

  • Question

  • User686469789 posted

    I am wanting to bring back only the appointments that match the date but the linq seems to be ignoring the appointmens cause of the time element I tried .date but it still seems to be ignorining that . I only want the list to get what is due from the parameter.

    I am calling the below function in this way  Does anybody  no what might be going wrong.

    The Data

      currentDate = Convert.ToDateTime("2012-02-14 11:45:00.000");
        sourceNetAppointments.FocusedDate = currentDate;
                schedulerBindingSource.EventProvider.DataSource = SourceDal.getAppointments(currentDate.Date);
     

             

     public  List<Appointment> getAppointments(DateTime apptDate)
            {
                List<Appointment> appointments = new List<Appointment>();
                try
                {
                    appointments= _sourceEntities.Appointments
                    .Where(a => a.ApptDate.Date == apptDate.Date) // Remove if not needed
                         .ToList();
                }
    
    
                
                catch (Exception EX)
                {
    
                }
                return appointments;
            }
    
    Thursday, December 21, 2017 7:45 PM

All replies

  • User2103319870 posted

    You can try with the below code

     public List<Appointment> getAppointments(DateTime apptDate)
            {
                List<Appointment> appointments = new List<Appointment>();
                try
                {
                    appointments = _sourceEntities.Appointments
                    .Where(a => DbFunctions.TruncateTime(a.ApptDate) == apptDate.Date) // Remove if not needed
                         .ToList();
                }
                catch (Exception EX)
                {
    
                }
                return appointments;
            }
    Thursday, December 21, 2017 8:14 PM
  • User475983607 posted

    One way is to convert the DateTime to a string like this...

            static void Main(string[] args)
            {
                DateTime currentDate = DateTime.Now;
    
                List<DateTime> dates = new List<DateTime>() {
                    DateTime.Now,
                    DateTime.Now.AddDays(-1),
                    DateTime.Now.AddDays(1),
                    DateTime.Now.AddMinutes(-100),
                    DateTime.Now.AddHours(5)
                };
                foreach (var date in dates)
                {
                    Console.WriteLine(date);
                }
                Console.WriteLine();
                IEnumerable<DateTime> query = dates.Where(m => m.ToShortDateString() == currentDate.ToShortDateString());
                foreach (var date in query)
                {
                    Console.WriteLine(date);
                }
    
                Console.ReadLine();
            }

    Another way is to compare the year, month, and day like so...

    IEnumerable<DateTime> query = dates.Where(m => m.Year == currentDate.Year & m.Month == currentDate.Month & m.Day == currentDate.Day);

    Another method is creating an extension method or utility that contains the comparison logic that way the logic is in one place.

    Thursday, December 21, 2017 8:16 PM
  • User1400794712 posted

    Hi david40ni,

    Do you mean that you want to get the Appointments which match the date, including year, month, day, hour, minute and second?

    If so just remove .Date, it will modify the time to 12:00:00.000. The code should be:

    currentDate = Convert.ToDateTime("2012-02-14 11:45:00.000");
    sourceNetAppointments.FocusedDate = currentDate;
    schedulerBindingSource.EventProvider.DataSource = SourceDal.getAppointments(currentDate);//Adjustment
    public  List<Appointment> getAppointments(DateTime apptDate)
    {
        List<Appointment> appointments = new List<Appointment>();
        try
        {
     appointments= _sourceEntities.Appointments
     .Where(a => a.ApptDate == apptDate) // Adjustment
                .ToList();
        }
        catch (Exception EX)
        {
        }
        return appointments;
    }

    Best Regards,
    Daisy

    Monday, December 25, 2017 9:37 AM