user2433270
user2433270

Reputation: 5

How to fetch record by weekly in entity framework

I would like to display records by weeks using entity framework for example if i pass 32 then i need to fetch records which have 32 week of this year.

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

I could not find week like year and month please help.

Thanks in Advance.

Upvotes: 0

Views: 338

Answers (1)

Rapha&#235;l Althaus
Rapha&#235;l Althaus

Reputation: 60503

with linq to entities, you should use 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
                   );

}

Upvotes: 1

Related Questions