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.
if it is only two levels (i.e. subtests can’t have subtests) then you can do this:
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.
now we use it and do our filter
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.
note that you still should add various null checks for completeness