entity framework - LINQ to Entities 4: Query with calculation in where clause -
i have table datetime "timestamp" column , int "timeoutseconds" column. want retrieve records table datetime.now - timestamp > timeoutseconds. in stored proc no brainer using getdate():
select * schema.tablename mp (getdate() - mp.[timestamp]) > mp.timeout
however, entity framework using either linq syntax or lambda cannot because seems lambda input variable (mp) cannot used part of calculation part of predicate, not compile:
var records = context.tablename.where(mp => (datetime.now - mp.timestamp) > mp.timeout);
this not compile.
i don't want retrieve whole table filtering in memory , rather not use stored proc or entity sql. options here?
this not compile because comparing (datetime.now - mp.timestamp)
has return type system.timespan
int
. first solution comes mind do
where(mp => (datetime.now - mp.timestamp) > new timespan(0, 0, 0, mp.timeout))
unfortunately doesn't seem work in ef, if have ms sql server db, can use sqlfunctions
in ef4:
var records = context. tablename. where(mp => system.data.objects.sqlclient.sqlfunctions. datediff("s", mp.timestamp, datetime.now) > mp.timeout);
Comments
Post a Comment