使用linq c#查找在特定日期范围内有效的记录

时间:2014-03-10 18:24:29

标签: c# asp.net-mvc linq

我需要列出一段时间内某个时间段内处于活动状态的记录:

Active的概念是DateActivation和DateDeactivation之间的范围。

型号:

public class DME
{
    public DateTime DateActivation { get; set; }               // Is always set
    public Nullable<DateTime> DateDeactivation { get; set; }   // Set or Null
}

动作:

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();

}

我没有让它正常工作,因为我无法在DateActivation&lt;例如,DateStart和DateDeactivation为null,或者DateActivation&gt; DateStart和DateDeactivation&gt; DateFinish

我认为有5个案例的记录在时间上是活跃的:

 DateActivation | DateStart | DateDeactivation  | DateFinish

 DateStart | DateActivation | DateDeactivation  | DateFinish

 DateStart | DateActivation | DateFinish | DateDeactivation

 DateActivation | DateStart | DateFinish | DateDeactivation = NULL

 DateStart | DateActivation | DateFinish | DateDeactivation = NULL

请帮忙吗?

感谢。

2 个答案:

答案 0 :(得分:1)

我认为这应该是您正在寻找的条件:

(d.DateActivation == null || dtStart < d.DateDeactivation) && dtEnd > d.DateActivation

答案 1 :(得分:0)

我会做这样的事情:

public class DME
{
    public DateTime DateActivation { get; set; }               // Is always set
    public DateTime? DateDeactivation { get; set; }   // Set or Null
}

public ActionResult ActiveRecordsInTimeFrame(string DateStart, string DateFinish)
{
    var model = GetActiveRecordsBetweenDates(
                    Convert.ToDateTime(DateStart), 
                    Convert.ToDateTime(DateFinish)).ToList();

    ...
}

...

private IQueryble<IEnumerable<int>> 
        GetActiveRecordsBetweenDates(DateTime dtStart, DateTime? dtEnd) 
{
    if(dtEnd.HasValue)
    {
        // both dates provided
        return from db in db.DMEs
               where d.DateActivation >= dtStart &&
                     d.DateActivation <= dtEnd
               select db.Id;
    }

    // no end date
    return from db in db.DMEs
           where d.DateActivation >= dtStart
           select db.Id;
}

评论更新:I only want the active ones.

并假设如果DateDeactivation有日期,那是因为它已经停用了

private IQueryble<IEnumerable<int>> 
        GetActiveRecordsBetweenDates(DateTime dtStart, DateTime? dtEnd) 
{
    if(dtEnd.HasValue)
    {
        // both dates provided
        return from db in db.DMEs
               where d.DateActivation >= dtStart &&
                     d.DateActivation <= dtEnd && 
                     d.DateDeactivation == null
               select db.Id;
    }

    // no end date
    return from db in db.DMEs
           where d.DateActivation >= dtStart && 
                 d.DateDeactivation == null
           select db.Id;
}