Classify values depending on predicates I've to do a generic method Classify&lt;T&gt; that, given a sequence of elements of type T and an arbitrary number of predicates, returns an array of n+1 lists (where n is the number of the predicates). More precisely, if the element satisfies the first predicate then it is added to the first list. If not, then if the element satisfies the second predicate it is added to the second list, etc. If none of the predicates can be applied to one element, then that element is added to the last list (the n+1 th).

I managed to do that with the following code. Is there any way to do it better, maybe with Linq?

public List&lt;T&gt;[] Classify&lt;T&gt;(IEnumerable&lt;T&gt; sequence, params Predicate&lt;T&gt;[] predicates)
{
    var result = new List&lt;T&gt;[predicates.Length+1];
    var counter = 0;    
    foreach (var elem in sequence)      
    {
        var valid = false;
        foreach (var predicate in predicates) 
        {
            if(result[counter]==null)
                result[counter] = new List&lt;T&gt;();
            if (predicate(elem) &amp;&amp; valid==false)
            {
                var partition = result[counter];
                partition.Add(elem);
                valid = true;
            }
            counter++;
        }
        if (!valid)     
        {
            if (result[counter] == null)
                result[counter] = new List&lt;T&gt;();
            result[counter].Add(elem);
        }
        counter=0;
    }
    return result;
}
public List&lt;T&gt;[] Classify&lt;T&gt;(IEnumerable&lt;T&gt; sequence, params Predicate&lt;T&gt;[] predicates)
{
    var result = new List&lt;T&gt;[predicates.Length + 1];
    // It's clearer to create elements of the array explicitly
    for (int i = 0; i &lt; result.Length; i++)
        result[i] = new List&lt;T&gt;();

    foreach (var elem in sequence)
    {
        var index = predicates.TakeWhile(p =&gt; !p(elem))
                              .Count();

        result[index].Add(elem);
    }
}


There is a few LINQ methods to get an index, f.e. Select((i, e) =&gt; ...). If the list of unsatisfied elements was the first (0th), I would use Select. But since it is the last, TakeWhile gives the shorter code.