Compare existence of record with date and future date

Asked

Viewed 44 times

0

I have the following model

public class AgendaExame
{
    public int Id {get; set;}
    public int PacienteId {get; set;}
    public int ExameId {get; set;}
    public DateTime Data {get; set;} 
    public DateTime ProximaData {get; set;}
}

The use of this model is to save the exam and date that was performed, and when will be the next exam (ProximaData)

Following this saved information:

ID, PacienteId, ExameId,         DATA,      PROXIMADATA
1      1             1        01/01/2018    01/01/2019  
2      1             2        01/01/2018    01/01/2019  
3      1             1        31/12/2018    31/12/2019

When I filter between 01/01/2019 à 31/01/2019 I need to seek the tests that certain patients need to perform.

However, as we see in line 3, the patient performed the exam 1 day in advance, so his next date is only on the day 31/12, then it should not be on the day 01/01

What I’ve tried so far is this:

  var busca = db.AgendaExames.AsQueryable()
                .Where(x =>
                     x.ProximaData != null &&
                     DbFunctions.TruncateTime(x.ProximaData ) >= model.Inicio &&
                     DbFunctions.TruncateTime(x.ProximaData ) <= model.Fim);

But this code only looks for the ones I have to perform, I still need to check if the patient did not take the exam in advance (as explained above)

Summary: Save exam and control when your next exam will be

  • What your query does is select all 'Scheduling Exams' that have 'Next Date' and also 'Date' between the end and the beginning of the date sent in the model. By what you tried to explain did not understand what you want, but will only return the 1st and 3rd record in this case if the date sent is 01/01/2018 to 31/12/2019.

  • @Georgewurthmann Yes, I believe only with Where will I not succeed. But it is as I explained, seek the exams to perform based on Proximadata, but I have to validate also, if he does not have "Proximadata" in future months...

  • Can you build a clearer example? In your code you compared both Proximadata and Data, but in this last comment you are comparing only

  • @Georgewurthmann ignore my code, he’s really wrong

1 answer

1

I don’t know exactly what you want to do with the information. But from what you explained in the comment you want to validate Proximadata is within a period and also if for this same field there is future date.

To bring this information you can make this change in the code that is in your question:

var busca = db.AgendaExames.AsQueryable()
.Where(x =>
     x.ProximaData != null &&
     DbFunctions.TruncateTime(x.ProximaData ) >= model.Inicio &&
     DbFunctions.TruncateTime(x.ProximaData ) <= model.Fim ||
     DbFunctions.TruncateTime(x.ProximaData ) > model.Fim);

But I didn’t see much point in it. If it’s something different try to explain your rules better.

More simply you could do just that which would bring all records within the informed period and future period as well:

var busca = db.AgendaExames.AsQueryable()
    .Where(x =>
         x.ProximaData != null &&
         DbFunctions.TruncateTime(x.ProximaData ) >= model.Inicio);
  • now it’s clearer to understand? I’m almost abandoning this solution and looking for an easier alternative

Browser other questions tagged

You are not signed in. Login or sign up in order to post.