Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 914893
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T17:47:39+00:00 2026-05-15T17:47:39+00:00

I have a class structure which looks like: class TestResults { public bool IsSuccess;

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T17:47:40+00:00Added an answer on May 15, 2026 at 5:47 pm

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a class structure for a role playing game which looks like this...
In C# I have an intrusive tree structure that looks like this: public abstract
I have a java class which fires custom java events. The structure of the
Let's say I have the following class structure: class Car; class FooCar : public
I have the following structure public class MyClass : MyBaseClass<System.Int32> { } In a
I have the following entity structure: public class Party { public Int32 PartyId {
I have a data structure that represents C# code like this: class Namespace: string
I have a Struts + Velocity structure like for example, a Person class, whose
Let's say I have data structures that're something like this: Public Class AttendenceRecord Public
I have the following structure. public class ToolSettings { public string Extension { get;

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.