Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/linq/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何知道使用LINQ的时段集合中是否存在重叠_C#_Linq - Fatal编程技术网

C# 如何知道使用LINQ的时段集合中是否存在重叠

C# 如何知道使用LINQ的时段集合中是否存在重叠,c#,linq,C#,Linq,我有一组句点[从日期到日期] 我想知道某个时期和收藏中的时期之间是否有重叠 我已经从这个开始了: // periodToCheck is the given item bool conflict = Periods.Any(p => ((p.FromDate >= periodToCheck.fromDate && p.FromDate <= periodToCheck.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))
                             );
如果我讨论更多的情况,我想问题会变得更复杂

我想知道你是否能帮我找到最简单有效的方法

尊敬。

您可能会发现有用的,尤其是
时间周期相交器

样本摘录:

public void TimePeriodIntersectorSample()
{
    TimePeriodCollection periods = new TimePeriodCollection();

    periods.Add( new TimeRange( new DateTime( 2011, 3, 01 ), new DateTime( 2011, 3, 10 ) ) );
    periods.Add( new TimeRange( new DateTime( 2011, 3, 05 ), new DateTime( 2011, 3, 15 ) ) );
    periods.Add( new TimeRange( new DateTime( 2011, 3, 12 ), new DateTime( 2011, 3, 18 ) ) );

    periods.Add( new TimeRange( new DateTime( 2011, 3, 20 ), new DateTime( 2011, 3, 24 ) ) );
    periods.Add( new TimeRange( new DateTime( 2011, 3, 22 ), new DateTime( 2011, 3, 28 ) ) );
    periods.Add( new TimeRange( new DateTime( 2011, 3, 24 ), new DateTime( 2011, 3, 26 ) ) );

    TimePeriodIntersector<TimeRange> periodIntersector = 
                    new TimePeriodIntersector<TimeRange>();
    ITimePeriodCollection intersectedPeriods = periodIntersector.IntersectPeriods( periods );

    foreach ( ITimePeriod intersectedPeriod in intersectedPeriods )
    {
        Console.WriteLine( "Intersected Period: " + intersectedPeriod );
    }
    // > Intersected Period: 05.03.2011 - 10.03.2011 | 5.00:00
    // > Intersected Period: 12.03.2011 - 15.03.2011 | 3.00:00
    // > Intersected Period: 22.03.2011 - 26.03.2011 | 4.00:00
} // TimePeriodIntersectorSample
public void timeperiod intersectorsample()
{
TimePeriodCollection periods=新的TimePeriodCollection();
添加(新的时间范围(新的日期时间(2011年3月1日),新的日期时间(2011年3月10日));
添加(新的时间范围(新的日期时间(2011年3月5日),新的日期时间(2011年3月15日));
添加(新的时间范围(新的日期时间(2011,3,12),新的日期时间(2011,3,18));
添加(新的时间范围(新的日期时间(2011,3,20),新的日期时间(2011,3,24));
添加(新的时间范围(新的日期时间(2011,3,22),新的日期时间(2011,3,28));
添加(新的时间范围(新的日期时间(2011,3,24),新的日期时间(2011,3,26));
时间周期相交器周期相交器=
新的TimePeriodIntersector();
ITimePeriodCollection intersectedPeriods=periodIntersector.IntersectPeriods(periods);
foreach(ITimePeriod intersectedPeriod in intersectedPeriods)
{
Console.WriteLine(“相交期间:+intersectedPeriod”);
}
//>交叉期:2011年3月5日-2011年3月10日| 5.00:00
//>交叉期:2011年3月12日-2011年3月15日| 3.00:00
//>交叉期:2011年3月22日-2011年3月26日| 4.00:00
}//TimePeriodIntersectorSample

相反,应该这样处理:如果检查日期的todate<起始日期,或者检查日期的fromdate>截止日期,则没有交互作用。这是我的支票。从!(check.ToDatep.ToDate)); 或(展开负片后):


Periods.Any(p=>check.ToDate>=p.FromDate&&check.FromDate是否有两个具有共同日期的时段,例如时段1的ToDate和时段2的FromDate是相同的,计算为交叉点

如果是,则对查询进行一点修改,仅检查某个期间的日期,如果单独在检查的期间内,就像其中一个日期在某个期间内,则存在交叉点:

bool conflict = Periods.Any(p => ((p.FromDate >= periodToCheck.fromDate && 
                                    p.ToDate <= periodToCheck.fromDate)
                                  ||
                                   (p.FromDate >= periodToCheck.toDate && 
                                    p.ToDate <= periodToCheck.toDate))
                             );
bool conflict=Periods.Any(p=>((p.FromDate>=periodToCheck.FromDate&&
p、 ToDate=周期TOCHECK.ToDate&&

p、 ToDateIf
FromDate@Gabe,+1:-)如果此代码嵌入在ASP.NET文件中,并用注释掉,那么我们也将使用相同的表达式!不幸的是,我不能混合使用C/C++代码,否则我还可以输入“->”…C中的
操作符与C/C++中的操作相同(请参阅)还有,别忘了
-->
@Gabe,我知道,但它需要非托管(不安全)要使用的代码->…但是
-->
上的链接谢谢,我会看一看。谢谢,这正是Stephen Chung所写的。
check.ToDate>=p.FromDate&&check.FromDate@Homam,我知道这一点,但我不只是想展示一个正确的公式;我还想用视觉的方式解释它背后的思想。第二,我想演示一下,通过一些富有表现力的助手方法,您可以获得一个易于理解的LINQ查询。非常感谢。我非常感谢。
Periods.Any(p => !(check.ToDate < p.FromDate || check.FromDate > p.ToDate));
Periods.Any(p => check.ToDate >= p.FromDate && check.FromDate <= p.ToDate));
bool conflict = Periods.Any(p => ((p.FromDate >= periodToCheck.fromDate && 
                                    p.ToDate <= periodToCheck.fromDate)
                                  ||
                                   (p.FromDate >= periodToCheck.toDate && 
                                    p.ToDate <= periodToCheck.toDate))
                             );
public static bool OverlapsWith(this Period a, Period b)
{
    return !(b.ToDate <= a.FromDate || a.ToDate <= b.FromDate);
}
//                         a
//                |-----------------|
//   |--------|                          |-----------|
//       b1                                    b2
           b.ToDate > a.FromDate && a.ToDate > b.FromDate
    Periods.Any(period => period.OverlapsWith(periodToCheck))