I'm trying to find an intersect on two collection where the items overlap having an inverse state.
public class Sample
{
public int SampleNumber { get; set; }
public DateTime StartTime { get; set; }
public DateTime EndTime { get; set; }
public bool SampleState { get; set; }
}
Example:
List<Sample> ListOfSamples1 = new List<Sample>();
List<Sample> ListOfSamples2 = new List<Sample>();
ListOfSamples1.Add( new Sample() {SampleNumber=1, StartTime = new DateTime(2018, 12, 1, 0, 0, 0), EndTime = new DateTime(2018, 12, 1, 0, 10, 0), SampleState= true });
ListOfSamples1.Add( new Sample() {SampleNumber=2, StartTime = new DateTime(2018, 12, 1, 0, 20, 0), EndTime = new DateTime(2018, 12, 1, 0, 30, 0), SampleState= false });
ListOfSamples2.Add( new Sample() {SampleNumber=3, StartTime = new DateTime(2018, 12, 1, 0, 5, 0), EndTime = new DateTime(2018, 12, 1, 0, 7, 0), SampleState= false});
ListOfSamples2.Add( new Sample() {SampleNumber=4, StartTime = new DateTime(2018, 12, 1, 0, 21, 0), EndTime = new DateTime(2018, 12, 1, 0, 22, 0), SampleState= true});
I would like to return samples from ListOfSamples2
that intersect with the samples that exist in ListOfSamples1
where the SampleState
is opposite.
For instance ListOfSamples2[0]
has a StartTime
and EndTime
that does reside in ListOfSamples1[0]
and their states are opposite.
I am doing this with a ForEach
but am looking for a more elegant way to do this that has more options.
Thank you