i got records in database. each record has field "date". given date=x need find records date value less x, next record date greater of x.
example:
id date -------------- 1 12.03.2013 2 15.03.2013 3 18.03.2013
now, got x=16.03.2013
, , need linq return record:
2 15.03.2013
but! x=15.03.2014
should return nothing (because there record smaller date, next record has same date x)
how can this?
the simplest approach imo find record would find, , check date afterwards:
var result = db.table .where(x => x.date <= target.date) .orderbydescending(x => x.date) .firstordefault(); if (result != null && result.date == target.date) { result = null; }
or could in query using secondary where
clause after filtering single result:
var result = db.table .where(x => x.date <= target.date) .orderbydescending(x => x.date) .take(1) .where(x => x.date != target.date) .firstordefault();
note doesn't work if values less x
(so there's no "next" record @ all). haven't yet worked out way handle that.