如何使用 LINQ 知道周期集合中是否存在重叠
我有一个时期[FromDate, ToDate]
的集合。
我想知道给定的时期和集合中的时期之间是否有重叠。
我已经开始了:
// periodToCheck is the given item
bool conflict = Periods.Any(p => ((p.FromDate >= periodToCheck.fromDate &&
p.FromDate <= periodToCheck.toDate)
||
(p.ToDate >= periodToCheck.fromDate &&
p.ToDate <= periodToCheck.toDate))
);
它没有涵盖所有情况的问题,例如:
[2010.1.1], [2010.1.31]
[2010.1.5], [2010.1.6] // Is valid in the query in spite of it is not valid
// (because there is intersection).
如果我讨论更多情况,我认为查询会变得更加复杂。
我想知道你是否可以用最简单有效的方法帮助我。
问候。
I have a collection of periods [FromDate, ToDate]
.
I would know whether there is any overlap between a given period and the periods in the collection.
I've already started with this:
// periodToCheck is the given item
bool conflict = Periods.Any(p => ((p.FromDate >= periodToCheck.fromDate &&
p.FromDate <= periodToCheck.toDate)
||
(p.ToDate >= periodToCheck.fromDate &&
p.ToDate <= periodToCheck.toDate))
);
The problem that it does not cover all the situation, for example:
[2010.1.1], [2010.1.31]
[2010.1.5], [2010.1.6] // Is valid in the query in spite of it is not valid
// (because there is intersection).
And if I discuss more situation I think the query will become more complicated.
I wonder if you could help me with the simplest valid way.
Regards.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
相反,可以这样处理:如果检查日期的 todate <<,则没有交互。起始日期,或检查日期的起始日期 >迄今为止。这是假设 check.from <= check.to。
或(打开底片后):
Approach it this way instead: There is no intersaction if the check date's todate < the from date, or the check date's fromdate > the to date. This is assuming check.from <= check.to.
or (after unwrapping the negative):
如果
FromDate <= ToDate
对于您的Period
对象始终成立,您可以定义一个帮助器 扩展方法OverlapsWith
如下:为了说明发生了什么,我们看一下在
a
和b
之间<em>没有重叠的两种情况:您可以对照此图检查上述情况。由于该图显示了没有发生重叠的情况,但该方法确实应该测试重叠,因此需要否定该条件。它可以简化为以下内容:
当您在 LINQ 查询中使用此方法时,结果非常容易理解:
If
FromDate <= ToDate
always holds true for yourPeriod
objects, you can define a helper extension methodOverlapsWith
as follows:To illustrate what's going on, let's look at the two cases where there is no overlap between
a
andb
:You can check the above condition against this diagram. Since the diagram shows the cases where no overlap occurs, but the method really ought to test for overlap, the condition needs to be negated. It could be simplified to the following:
When you use this method in a LINQ query, it turns out very easy to understand:
您可能会发现以下文章很有用,尤其是
TimePeriodIntersector
类。样本摘录:
You may find the following article useful and especially the
TimePeriodIntersector
class.Sample excerpt:
两个具有共同日期的时间段(例如时间段 1 的 ToDate 和时间段 2 的 FromDate)是否相同,算作交集吗?
如果是,那么对您的查询进行一些修改,以简单地检查一个期间的日期是否在单独检查的期间内,就好像其中一个日期落在一个期间内,然后存在交集:
Do 2 periods having common date, say ToDate for period 1 and FromDate of period 2 are the same, count as in intersection ?
If yes then a little modification to your query to simply check the dates of a period if are within the checked period separately as if one of the dates falls inside a period then there is intersection: