Sélectionnez la semaine en cours en utilisant LINQ
Question
Comment puis-je écrire l'instruction où
qui sélectionne les enregistrements avec le champ Date
entre dimanche
et samedi
d'un date donnée.
Data Fields: Id, Name, Date
La solution
Où date
est la date en question, qu'en est-il:
DateTime start = date.Date.AddDays(-(int)date.DayOfWeek), // prev sunday 00:00
end = start.AddDays(7); // next sunday 00:00
var qry = from record in data
where record.Date >= start // include start
&& record.Date < end // exclude end
select record;
Autres conseils
DateTime givenDate = DateTime.Today;
DateTime startOfWeek = givenDate.AddDays(-1 * givenDate.DayOfWeek);
DateTime endOfWeek = startOfWeek.AddDays(7);
var query = myObjects
.Where(ob => startOfWeek <= ob.DateField && ob.DateField < endOfWeek)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow