简体   繁体   English

根据数据表中的日期范围集检查日期范围

[英]Checking date range against set of date ranges in a data table

I need to check a user-entered start and end date against a set of start and end dates in a data table to make sure there are no overlaps. 我需要对照数据表中的一组开始日期和结束日期检查用户输入的开始日期和结束日期,以确保没有重叠。

A user requests time off using a start and end date combination. 用户使用开始日期和结束日期组合请求休假。 I want to make sure this start and end date is not contained in a set of dates I have read from DB into a data table. 我想确保这个开始日期和结束日期不包含在我从数据库读入数据表的一组日期中。

I have used the following but not sure if this is correct. 我使用了以下内容,但不确定是否正确。 Here "table" contains user's existing time off requests taken from DB, startDate and endDate is what s/he is requesting. 这里的“表”包含用户从数据库中获取的现有休假请求,startDate和endDate是他/她所请求的。 Data table has "StartDate" and "EndDate" columns. 数据表具有“ StartDate”和“ EndDate”列。

private DataTable FilterTable(DataTable table, DateTime startDate, DateTime endDate)
{
    var filteredRows =
        from row in table.Rows.OfType<DataRow>()
        where (DateTime)row["StartDate"] >= startDate
        where (DateTime)row["StartDate"] <= endDate
        select row;

    var filteredTable = table.Clone();
    filteredRows.ToList().ForEach(r => filteredTable.ImportRow(r));
    return filteredTable;
}

If the returned data table has no rows, it is OK otherwise there is overlap. 如果返回的数据表没有行,则可以,否则存在重叠。

Using an extension method to check if a Date is between two others, 使用扩展方法检查日期是否在其他两个日期之间,

public static class DateTimeExt {
    public static bool Between(this DateTime aDate, DateTime start, DateTime end) => start <= aDate && aDate <= end;
}

You can write an Overlaps method that determines if two ranges overlap: 您可以编写一个Overlaps方法来确定两个范围是否重叠:

public static bool Overlaps(DateTime aPeriodStart, DateTime aPeriodEnd, DateTime bPeriodStart, DateTime bPeriodEnd)
    => aPeriodStart.Between(bPeriodStart, bPeriodEnd) ||
       aPeriodEnd.Between(bPeriodStart, bPeriodEnd) ||
       bPeriodStart.Between(aPeriodStart, aPeriodEnd);

Now with another extension method that converts an IEnumerable<DataRow> to a DataTable containing the rows: 现在有了另一个扩展方法,该方法将IEnumerable<DataRow>转换为包含行的DataTable

public static class IEnumerableExt {
    public static DataTable ToDataTable(this IEnumerable<DataRow> src) {
        var ans = src.First().Table.Clone();
        foreach (var r in src)
            ans.ImportRow(r);
        return ans;
    }    
}

Your final method is straight forward: 您的最终方法很简单:

DataTable FilterTable(DataTable timeTable, DateTime startDate, DateTime endDate) =>
    timeTable.AsEnumerable().Where(period => Overlaps(period.Field<DateTime>("StartDate"), period.Field<DateTime>("EndDate"), startDate, endDate)).ToDataTable();

NOTE: If you don't need the answering DataTable for anything, it would be more efficient to replace .ToDataTable() with .Any() and just have the method return a bool indicating if any overlaps exist. 注:如果您不需要应答DataTable的话,这会更有效,以取代.ToDataTable().Any()只是有方法返回一个bool指示是否存在任何重叠。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM