tags:

views:

54

answers:

3

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.

A: 

Try this:

var failures = from result in results
               where result.GetType().GetProperty("IsFailure") != null
               select result;

Alternatively, have your test classes inherit from a common base class, for example TestBase, and then write your query like this:

var failures = from result in results
               where result is TestBase
               select result;

EDIT: Hmm, I think I misunderstood your question on the first read-through

Dr. Wily's Apprentice
+3  A: 

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

luke
+2  A: 

You need a recursive expression to do that, so given this:

Func<TestResult, IEnumerable<TestResult>> func = null;
func = r => r.Tests == null
            ? Enumerable.Empty<TestResult>()
            : r.Tests.Where(t => t.IsFailure)
                     .Union(r.Tests.SelectMany(r2 => func(r2)));

var failures = func(tree);

The only thing you need to do, is include the root item if it has failed. You'll hopefully notice the statement is split between declaration and assignment, this is because a compiler error will occur if you try and assign a recursive delegate straight at the declaration.

Matthew Abbott