Currently I fetch all customers that have registered exactly 2 years before a given date.
var observationPeriod = DateTime.Today;
var observedDate = observationPeriod.AddYears(-2);
var customers = unitOfWork.CustomerRepository
.GetAll()
.Where(o => o.RegistrationDate.Date == observedDate)
.ToList();
But my requirement is to get all customers that registered 2x years before, e.g. 2, 4, 6, etc. years. Ideally, this should also be entity framework core compatible. What would you recommend?
Currently I fetch all customers that have registered exactly 2 years before a given date.
var observationPeriod = DateTime.Today;
var observedDate = observationPeriod.AddYears(-2);
var customers = unitOfWork.CustomerRepository
.GetAll()
.Where(o => o.RegistrationDate.Date == observedDate)
.ToList();
But my requirement is to get all customers that registered 2x years before, e.g. 2, 4, 6, etc. years. Ideally, this should also be entity framework core compatible. What would you recommend?
Share Improve this question edited Mar 15 at 18:57 AGuyCalledGerald asked Mar 13 at 9:36 AGuyCalledGeraldAGuyCalledGerald 8,17018 gold badges77 silver badges123 bronze badges 6 | Show 1 more comment1 Answer
Reset to default 4You can check if the difference between the registration date and the observation period is a multiple of 2 years
var observationPeriod = DateTime.Today;
var customers = unitOfWork.CustomerRepository
.GetAll()
.Where(o => (observationPeriod.Year - o.RegistrationDate.Year) % 2 == 0 &&
o.RegistrationDate.Date == observationPeriod.AddYears(-(observationPeriod.Year - o.RegistrationDate.Year)).Date &&
o.RegistrationDate <= observationPeriod)
.ToList();
.Contains(...)
– JonasH Commented Mar 13 at 9:41