admin管理员组文章数量:1394150
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();
本文标签: cget dates exactly 2x years before given dateStack Overflow
版权声明:本文标题:c# - get dates exactly 2x years before given date - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744710595a2621103.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
.Contains(...)
– JonasH Commented Mar 13 at 9:41