asp.net mvc - Find records that were active in a certain date range using linq c# -
i need list records active in point in period of time:
the concept of active here range between dateactivation , datedeactivation.
model:
public class dme { public datetime dateactivation { get; set; } // set public nullable<datetime> datedeactivation { get; set; } // set or null }
action:
public actionresult activerecordsintimeframe(string datestart, string datefinish) { ilist<int> dmeids = db.dmes .asenumerable() .where(d => d.dateactivation >= convert.todatetime(datestart)) .where(d => d.dateactivation <= convert.todatetime(datefinish)) .where(d => d.datedeactivation == null || d.datedeactivation <= convert.todatetime(datefinish)) .select(d => d.id).tolist(); }
i not getting work properly, because can't records when dateactivation < datestart , datedeactivation null example, or when dateactivation > datestart , datedeactivation > datefinish
i think there 5 cases records active in time:
dateactivation | datestart | datedeactivation | datefinish datestart | dateactivation | datedeactivation | datefinish datestart | dateactivation | datefinish | datedeactivation dateactivation | datestart | datefinish | datedeactivation = null datestart | dateactivation | datefinish | datedeactivation = null
any please?
thanks.
i think should condition you're looking for:
(d.dateactivation == null || dtstart < d.datedeactivation) && dtend > d.dateactivation
Comments
Post a Comment