开发者

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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜