Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Entity framework DateTime.Now - 7 days C#

I try to get the entries of my mssql table from the past 7 days with entity framework in c#.

For that i tried:

var query = context.tbl.Where(fld => fld.date >= (DateTime.Now.AddDays(-7)));

This doesn't work at all. I thought that if the date from the record was bigger or equal than date.now - 7 days, it should give me back all entries from the past 7 days.

like image 804
nova.cp Avatar asked Feb 26 '14 10:02

nova.cp


1 Answers

DateTime.AddDays() cannot be converted to a store expression by Entity Framework. I'm assuming this is what you mean when you say it doesn't work?

Try this:

var dateTime = DateTime.Now.AddDays(-7);
var query = context.tbl.Where(fld => fld.date >= dateTime);
like image 116
Adam Drewery Avatar answered Oct 14 '22 23:10

Adam Drewery