Compare existence of record with date and future date

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 done, and when will 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 and 31/01/2019 I need to search for the exams certain patients need to perform.

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

What I've tried so far was 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 searches 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 track when your next exam will be

    
asked by anonymous 28.11.2018 / 20:14

1 answer

1

I do not know exactly what you want to do with the information. But for what you explained in the comment you want to validate from the ProximaData is within a period and also for that same field there is a 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 did not see much sense in this. If it's something different try to explain your rules better.

More simplistically 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);
    
29.11.2018 / 11:28