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

  • SEARCH
  • Home
  • 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 8577931
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:16:16+00:00 2026-06-11T20:16:16+00:00

I have a question for you linq experts out there! In a nested list

  • 0

I have a question for you linq experts out there!
In a nested list of Component instances, I need to know if there is a component of a particular type in it. Can it be expressed by linq? Take into account that there may be application.Components[0].Components[0].Components[0]… My question is oriented to recursive queries in linq!

I leave you the entities for you to have some idea of the model.

public class Application
{
    public List<Component> Components { get; set; }
}

public class Component
{
    public ComponentType Type { get; set; }
    public List<Component> Components { get; set; }
}

public enum ComponentType
{
    WindowsService,
    WebApplication,
    WebService,
    ComponentGroup
}
  • 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-06-11T20:16:18+00:00Added an answer on June 11, 2026 at 8:16 pm

    You want to know if any of the components in a component is of a given type?

    var webType = ComponentType.WebApplication;
    IEnumerable<Component> webApps = from c in components
                                     from innerComp in c.Components
                                     where innerComp.Type == webType;
    bool anyWebApp = webApps.Any();
    

    what about innercomp.components?

    Edit: So you want to find components of a given type recursively not only on the top or second level. Then you can use following Traverse extension method:

    public static IEnumerable<T> Traverse<T>(this IEnumerable<T> source, Func<T, IEnumerable<T>> fnRecurse)
    {
        foreach (T item in source)
        {
            yield return item;
    
            IEnumerable<T> seqRecurse = fnRecurse(item);
            if (seqRecurse != null)
            {
                foreach (T itemRecurse in Traverse(seqRecurse, fnRecurse))
                {
                    yield return itemRecurse;
                }
            }
        }
    }
    

    to be used in this way:

    var webType = ComponentType.WebApplication;
    IEnumerable<Component> webApps = components.Traverse(c => c.Components)
                                     .Where(c => c.Type == webType);
    bool anyWebApp = webApps.Any();
    

    sample data:

    var components = new List<Component>() { 
        new Component(){ Type=ComponentType.WebService,Components=null },
        new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
            new Component(){ Type=ComponentType.WebService,Components=null },
            new Component(){ Type=ComponentType.ComponentGroup,Components=null },
            new Component(){ Type=ComponentType.WindowsService,Components=null },
        } },
        new Component(){ Type=ComponentType.WebService,Components=null },
        new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
            new Component(){ Type=ComponentType.WebService,Components=new List<Component>(){
                new Component(){Type=ComponentType.WebApplication,Components=null}
            } },
            new Component(){ Type=ComponentType.WindowsService,Components=null },
            new Component(){ Type=ComponentType.WebService,Components=null },
        } },
        new Component(){ Type=ComponentType.WebService,Components=null },
        new Component(){ Type=ComponentType.ComponentGroup,Components=null },
        new Component(){ Type=ComponentType.WebService,Components=null },
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a basic LINQ question. I have need to select one object
I have a question about LINQ query. Normally a query returns a IEnumerable<T> type.
Another Linq question =) So I have a certain interface that I can code
I have a newbie LINQ question. I need to create two objects of same
I have one question about LINQ, I dont know how to get the result
I have a very similar question to: LINQ to read XML I need to
Hi Experts I have a special question About dynamic Linq to Sql. Consider we
I have a Question class that has public List property that can contain several
I have a similar issue as in this question: linq question: querying nested collections
I have a question regarding the integration of business objects developed using Linq To

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.