在linq C#中获取重叠的时间范围 [英] Get overlapping time range in linq C#

查看:233
本文介绍了在linq C#中获取重叠的时间范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在做一些重叠范围测试并且已成功结束重叠日期范围方案,但另一方面我必须检查重叠时间范围,而我无法执行该操作。我的情况如下,



我有上午8点到下午5点的范围和第二个晚上8点到9点



我试过像3



I have been doing some overlapping ranges test and have successfully concluded the overlapping date ranges scenario, But on the other hand I have to check for the overlapping time ranges, and I am unable to perform that. I have a scenario as below,

I have a range of 8am to 5pm and second range of 8 pm to 9am

I have tried like 3

bool overlap = a.start < b.end && b.start < a.end;





和预期结果在晚上8点到9点范围内是早上8点重叠,但由于日期部分相关,它会造成麻烦。





任意想法或指导解决这个问题?



我尝试过:





And the expected results are true as 8am overlapps in the 8pm to 9am range, but because of the date part associated, it causing the trouble.


Any ideas or guide to solve this?

What I have tried:

bool overlap = a.start < b.end && b.start < a.end;

推荐答案

试试这个:



Try this:

bool overlap = a.start.TimeOfDay < b.end.TimeOfDay && b.start.TimeOfDay < a.end.TimeOfDay;


我认为a.start和a.end是 DateTime / DateTimeOffset 实例。

使用 a.start.TimeOfDay DateTimes中的属性从日期获取 TimeSpan (忽略计算中的日期部分)



像这样:

I asume a.start and a.end are DateTime/DateTimeOffset instances.
Use the a.start.TimeOfDay property from your DateTimes to get an TimeSpan from the dates (to ignore the date part in the calculation)

like this:
bool overlap = a.start.TimeOfDay < b.end.TimeOfDay  && b.start.TimeOfDay  < a.end.TimeOfDay;





或者只是将时间存储在第一位,例如像这样,那么你的比较也会起作用





or just store the times in the first place e.g like this then your comparison would also work

DateTime myTime = default(DateTime).Add(myDateTime.TimeOfDay);


假设我们的日期范围 a 其开始和结束时间如下



Suppose we Have a date range a which start and end time are as below

a.start = '1900-01-01 08:00:00.000'; i.e 8am
a.end = '1900-01-01 17:00:00.000'; i.e. 5pm







我们要检查它是否与现有范围重叠。

假设前一个范围之一是 b ,其开头和结束时间如下,






and we want to check it either it overlaps with the existing ranges or not.
suppose one of the previous range is b which have start and end time like below,

b.start = '1900-01-01 23:00:00.000'; i.e 11pm
b.end = '1900-01-01 09:00:00.000'; i.e. 9am





所以b的重叠时间为早上8点到早上9点,因为时间段是跨日的,所以我找到了重叠如下。





so b has a overlapping period of 8am to 9am with a, as the time slots are cross-day so I find the overlapping like below.

bool isOverLapping = false;
a = FirstRange;

double basehours = (a.end - a.start).TotalHours;

if (basehours <= 0)
{
    basehours = basehours + 24;
}


foreach(var b in previousRanges)
{
 	double alpha1 = (b.start - a.start).TotalHours;

	if (alpha1 <= 0)
	{
	  alpha1 = alpha1 + 24;
	}

	double alpha2 = (b.end - a.start).TotalHours;

	if (alpha2 <= 0)
	{
	  alpha2 = alpha2 + 24;
	}

	if (basehours <= alpha1 && basehours <= alpha2)
	{
	  isOverLapping = false;
	}
	else
	{
	  isOverLapping = true;	
	}
}







我希望它可以帮助那些面对像我这样的问题。




I hope it will help someone who faces the issue like i did.


这篇关于在linq C#中获取重叠的时间范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆