I’m trying do some manual validation for data in a List.
I have a MachineryRecord class which I sort then group by the JobNumber, now I need to find any results in each of the grouped lists that have overlapping times eg. Start time to End time overlaps another records StartTime to EndTime.
So far this is what I have got:
int invalidCount = 0;
var sorted = _machineRecords.OrderBy(x => x.StartTime).ToList();
var grouped = sorted.GroupBy(x => x.JobNumber).ToList();
foreach(IGrouping<int,MachineryRecord> mac in grouped)
{
var queryResults = mac.//Linq query to find overlapping times
invalidCount += queryResults.Count;
}
if(invalidCount > 0)
return false;
else
return true;
and heres a cutdown version of MachineRecord object:
public class MachineryRecord
{
public int ID { get; set; }
public float StartTime { get; set; }
public float EndTime { get; set; }
}
So my question is what is the linq query to achieve this?
Thanks for the help.
Two intervals
aandboverlap ifa.StartTime < b.EndTimeanda.EndTime > b.StartTimeassuming the start time is always before the end time. Therefore this can be expressed as follows.Including the final check of
invalidCountthis can be simplified to a single return statement.