使用LINQ选择当前周

时间:2009-06-23 20:23:08

标签: c# linq date where dayofweek

如何编写where语句,在指定日期的DateSunday之间选择包含Saturday字段的记录。

Data Fields: Id, Name, Date

2 个答案:

答案 0 :(得分:13)

date是有问题的日期,如何:

    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;

答案 1 :(得分:1)

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)