Search code examples
c#linq

Get all objects from a tree with a particular property using Linq


I have a class structure which looks like:

class TestResults {
   public bool IsSuccess;
   public bool IsFailure;

   public IList<TestResults> SubTestResults;
}

So, a test has multiple subtests, and the above captures the results.

I want to find all of the TestResults which have IsFailure = true.

Starting with:

var failures = from result in results
               where result.IsFailure
               select result;

This gives me the top level results which have IsFailure=true, but I want to get all of the tests, and subtests, which have IsFailure=true, and have all of these in list. Is there a linq query that can do this, or should I be using old-fashioned loops?

Update: I should say the my usage of these classes means the tree is only 2-deep (structure of the classes is not under my control).

So I have:

Test1 - SubTest11
        SubTest12
        SubTest13

Test2 - SubTest21
        SubTest22

Test3 - SubTest31
        SubTest32
        SubTest33
        SubTest34

I need all those subtests which have IsFailure = true.


Solution

  • if it is only two levels (i.e. subtests can't have subtests) then you can do this:

    var failures = results.Union(results.SelectMany(r => r.SubTestResults))
                          .Where(r => r.IsFailure);
    

    This takes the list of results and unions it with the list of all the results children.

    if you can have an arbitrary depth of subtests then you will need something more complicated, so we first define a helper function for the recursion.

    IEnumerable<TestResults> AllResults(IEnumerable<TestResults> results)
    {
        foreach(var tr in results)
        {
            yield return tr;
            foreach(var sr in AllResults(tr.SubTests))
                yield return sr;
        }
    }
    

    now we use it and do our filter

    var failures = from result in AllResults(results)
                   where result.IsFailure
                   select results;
    

    this should do the trick. however my implementation of AllResults above is not particularly efficient (see Wes Dyers blog for details) so you really would want to do the typical recursive to iterative conversion on AllResults.

    IEnumerable<TestResults> AllResults(IEnumerable<TestResults> results)
    {
        var queued = new Queue<TestResult>(results); 
        while(queued.Count > 0)
        {
            var tr = queued.Dequeue();
            yield return tr;
            foreach(var sr in tr.SubTests)
                queued.Enqueue(sr);
        }
    }
    

    note that you still should add various null checks for completeness