如何在实体框架中每周获取记录

时间:2013-05-29 16:07:57

标签: c# entity-framework datetime

我想使用实体框架按周显示记录,例如,如果我通过32,那么我需要获取今年32周的记录。

public List<Customer> ByWeek(int year,int week)
 {
  return db.Customers.where(p=>p.Createon.Year==year);
 }

我无法找到像年月一样的周,请帮助。

先谢谢。

1 个答案:

答案 0 :(得分:1)

使用linq到实体,您应该使用SqlFunctions

public List<Customer> ByWeek(int year, int week) {
   return db.Customers.Where(p => 
                   SqlFunctions.DatePart("week", p.Createon) == week && 
                   SqlFunctions.DatePart("year", p.Createon) == year
                   );

}