Home > OS >  DateTime comparing in Xamarin.Forms
DateTime comparing in Xamarin.Forms

Time:05-05

I have a appointments service which return a list of appointments, I want to return just upcoming appointment from firebase database so I did this :

public static async Task<IEnumerable<Appoitment>> GetUserAppointmentsByDate(DateTime? dateTime , string str)
{
    var Patients = await PatientService.GetUserPatients();

    if (Patients == null)
        return new List<Appoitment>();
    else
    {
        List<Appoitment> appointments = new List<Appoitment>();

        foreach (var patient in Patients)
        {
            if (patient.Appointments != null)
                appointments.AddRange(patient.Appointments.Values);
        }

        if (appointments == null && str == "all")
            return new List<Appoitment>();
        else if (appointments == null && str == "up")
            return (appointments.Where(o => o.AppointmentDate.Date.Date > DateTime.Now.Date));
        else if (dateTime.HasValue)
            return (appointments.Where(o => o.AppointmentDate.Date.Date == dateTime.Value.Date));
        else
            return appointments;
    }
}

And then I call it like this:

Appoitments = new ObservableCollection<Appoitment>(await AppintmentService.GetUserAppointmentsByDate(null,"up"));

but when list displayed, it's return all appointments without filtering them, so what is the problem and how can I fix it?

CodePudding user response:

this is wrong

else if (appointments == null && str == "up")

it should be

else if (appointments != null && str == "up")
  • Related