建立一个最有效的查询来查找日期范围是否在一个区间内?

发布于 2024-10-21 01:32:14 字数 437 浏览 4 评论 0原文

DateRange1 和 DateRange2 之间存在重叠

DateRange1:        |-----------|
DateRange2:                |-------|

DateRange3 和 DateRange4 之间不存在重叠

DateRange3:    |------|
DateRange4:                 |-------|

我有一个查询来检查是否存在重叠,但它有点毛茸茸的。它涉及检查每个 DateRange 边框并使用大量 < 和 > 。

我想知道你是否可以说,如果 DateRange1.Union(DateRange2) != null,做一些事情。 只是让我的代码更具可读性并且更少毛茸茸的东西。

谢谢!

Overlap exists between DateRange1 and DateRange2

DateRange1:        |-----------|
DateRange2:                |-------|

Overlap does not exist between DateRange3 and DateRange4

DateRange3:    |------|
DateRange4:                 |-------|

I have a query that checks if there is an overlap, but it's a little hairy. It involves checking each DateRange border and uses a lot of <'s and >'s.

I was wondering if you could say like, if DateRange1.Union(DateRange2) != null, do stuff.
Just something to make my code more readable and less hairy.

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

恋竹姑娘 2024-10-28 01:32:14

如果您可以添加 IncludedDates 属性 (IEnumerable),那应该非常简单。

DateRange1.IncludedDates.Intersect(DateRange2.IncludedDates).Any()

If you can add a IncludedDates property (IEnumerable<DateTime>), it should be pretty easy.

DateRange1.IncludedDates.Intersect(DateRange2.IncludedDates).Any()
丢了幸福的猪 2024-10-28 01:32:14

我将创建一个扩展方法:

public static bool OverlappedTo(this DateRange range1, DateRange range2)
{
    return range1.OverlappedTo(range2.Start, range2.End);
}

// Just an example, implement as you prefer...
public bool OverlappedTo(this DateRange range1, DateRange range2)
{
    var duration1 = range1.End - range1.Start;
    var duration2 = range2.End - range2.Start;

    var minStart = range1.Start < range2.Start ? range1.Start : range2.Start;
    var maxEnd = range1.End > range2.End ? range1.End : range2.End;

    var totalDuration = maxEnd - minStart;

    // if the sum of the 2 durations is less than 
    // the total duration --> overlapped
    return duration1 + duration2 < totalDuration;
}

用法:

bool overlapped = range1.OverlappedTo(range2);

PS

我的 DateRange 类类似于:
(也许你有另一个实现)

public class DateRange
{
    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }

    public DateRange(DateTime start, DateTime end)
    {
        this.Start = start;
        this.End = end;
    }
}

编辑:
(根据评论)

var outSideRange = new DateRange(start, end);

var filteredRows = 
dt.AsEnumerable()
  .Where(x => outSideRange.OverlappedTo(x.Field<DateTime>("StartCol"),
                                        x.Field<DateTime>("EndCol")));

I'd create an extension method:

public static bool OverlappedTo(this DateRange range1, DateRange range2)
{
    return range1.OverlappedTo(range2.Start, range2.End);
}

// Just an example, implement as you prefer...
public bool OverlappedTo(this DateRange range1, DateRange range2)
{
    var duration1 = range1.End - range1.Start;
    var duration2 = range2.End - range2.Start;

    var minStart = range1.Start < range2.Start ? range1.Start : range2.Start;
    var maxEnd = range1.End > range2.End ? range1.End : range2.End;

    var totalDuration = maxEnd - minStart;

    // if the sum of the 2 durations is less than 
    // the total duration --> overlapped
    return duration1 + duration2 < totalDuration;
}

Usage:

bool overlapped = range1.OverlappedTo(range2);

P.S.

My DateRange class resemble to this:
(perhaps you have another implementation)

public class DateRange
{
    public DateTime Start { get; private set; }
    public DateTime End { get; private set; }

    public DateRange(DateTime start, DateTime end)
    {
        this.Start = start;
        this.End = end;
    }
}

EDIT:
(according to the comments)

var outSideRange = new DateRange(start, end);

var filteredRows = 
dt.AsEnumerable()
  .Where(x => outSideRange.OverlappedTo(x.Field<DateTime>("StartCol"),
                                        x.Field<DateTime>("EndCol")));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文