Comparing two dates by week in a .NET entity framework LINQ query?
Is there an easy way of compar开发者_Go百科ing weeks in two dates like this...
var times = (from d in db.TimeSet
where d.EventDate.Week) == DateTime.ThisWeek &&
d.Employee.Username == username
select d).ToList();
public static int GetWeekNumber(DateTime date)
{
CultureInfo ci = CultureInfo.CurrentCulture;
int weekNum = ci.Calendar.GetWeekOfYear(date, CalendarWeekRule.FirstFourDayWeek, DayOfWeek.Monday);
return weekNum;
}
and
var times = (from d in db.TimeSet
where GetWeekNumber(d.EventDate) == GetWeekNumber(DateTime.Now) &&
d.Employee.Username == username
select d).ToList();
You might want to look into CalendarWeekRule to pick the one that fits your needs.
精彩评论