This is my Result
class:
public class Result
{
public DateTime Checked { get; set; }
public bool IsWorking { get; set; }
}
Let's say I have theese results in list:
Date: ..., IsWorking = true;
Date: ..., IsWorking = true;
Date: ..., IsWorking = false;
Date: ..., IsWorking = false;
Date: ..., IsWorking = true;
Date: ..., IsWorking = false;
Date: ..., IsWorking = true;
Date: ..., IsWorking = true;
I want to split them as following:
// group 1
Date: ..., IsWorking = true;
Date: ..., IsWorking = true;
// group 2
Date: ..., IsWorking = false;
Date: ..., IsWorking = false;
// group 3
Date: ..., IsWorking = true;
// group 4
Date: ..., IsWorking = false;
// group 5
Date: ..., IsWorking = true;
Date: ..., IsWorking = true;
I've managed something like this:
public static List<List<T>> SplitByEqualProperty<T>(this IEnumerable<T> inputs, string property)
{
List<List<T>> temp = new List<List<T>>();
temp.Add(new List<T>());
object previousSelector = null;
for (int i = 0; i < inputs.Count(); i++)
{
var current = inputs.ElementAt(i);
Type t = current.GetType();
PropertyInfo prop = t.GetProperty(property);
object currentSelector = prop.GetValue(current);
if (previousSelector == null)
{
temp.LastOrDefault().Add(current);
}
else
{
if (currentSelector.Equals(previousSelector))
{
temp.LastOrDefault().Add(current);
}
else
{
temp.Add(new List<T>() { current });
}
}
previousSelector = currentSelector;
}
return temp;
}
And this is working, but I don't like the code. Expecially part with string property
. How can I improve that?