Group list into elements with same value 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&lt;List&lt;T&gt;&gt; SplitByEqualProperty&lt;T&gt;(this IEnumerable&lt;T&gt; inputs, string property)
{
    List&lt;List&lt;T&gt;&gt; temp = new List&lt;List&lt;T&gt;&gt;();
    temp.Add(new List&lt;T&gt;());

    object previousSelector = null;
    for (int i = 0; i &lt; 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&lt;T&gt;() { 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?
You've tagged this linq so you're clearly aware of Linq, but this is not a Linq-like approach. The closest approximation in Linq is GroupBy, so that can serve as a model:


Instead of string property take Func&lt;TSource, TKey&gt;.
Instead of List&lt;List&lt;T&gt;&gt; (which, apart from anything else, violates the principle of coding to the interface instead of the implementation) return IEnumerable&lt;IGrouping&lt;TKey, TSource&gt;&gt; (or, if you don't care about the value itself, IEnumerable&lt;IEnumerable&lt;TSource&gt;&gt;).


Also, in Linq style, favour lazy implementation with yield return.

I would advise you to rewrite from scratch with those principles in mind.



But I must address one major problem in this code, lest you repeat it in the rewrite:


    for (int i = 0; i &lt; inputs.Count(); i++)
    {
        var current = inputs.ElementAt(i);



This is absolutely the wrong way to iterate over an IEnumerable. The right way is

    foreach (var current in inputs)
    {


Otherwise if inputs is lazy you do far more work than necessary, and potentially execute side-effects more times than you might expect.



Also,


    if (previousSelector == null)



is problematic. What if this method is used with objects which actually have null as a value of the relevant property? I think the best approach is to use the current IGrouping: either initialise that to null and test whether it's null, or initialise it to a grouping with key default(T) and replace the special case with a special case which doesn't return an empty grouping.