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 8575049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T19:36:34+00:00 2026-06-11T19:36:34+00:00

I have a generic method which adds all the html controls I have on

  • 0

I have a generic method which adds all the html controls I have on a page into a generic list using a series of foreach loops which works. Is it possible to convert this into a LINQ expression?

private List<T> GetControls<T>() where T : HtmlControl
         {
            List<T> c = new List<T>();

            foreach (HtmlControl c1 in Controls)
            {
                foreach (HtmlControl c2 in c1.Controls)
                {
                    if (c2.GetType() == typeof(HtmlForm))
                    {
                        foreach (Control c3 in c2.Controls)
                        {
                            if (c3.GetType() == typeof(ContentPlaceHolder))
                            {
                                foreach (HtmlControl c4 in c3.Controls)
                                {
                                    if (c4.GetType() == typeof(T))
                                    {
                                        c.Add((T)c4);
                                    }
                                    if (c4.GetType() == typeof(PlaceHolder))
                                    {
                                        foreach (HtmlControl c5 in c4.Controls)
                                        {
                                            if (c5.GetType() == typeof(T))
                                            {
                                                c.Add((T)c5);
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return c;
        }
  • 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-11T19:36:35+00:00Added an answer on June 11, 2026 at 7:36 pm

    This should do it:

    List<T> c = this.Controls.Cast<Control>()
        .SelectMany(c1 => c1.Controls.Cast<Control>())
        .OfType<HtmlForm>()
        .SelectMany(c2 => c2.Controls.Cast<Control>())
        .OfType<ContentPlaceHolder>()
        .SelectMany(c3 => c3.Controls.Cast<Control>())
        .SelectMany(c4 =>
            {
                if (c4 is T)
                    return new[] { (T)c4 };
                if (c4 is PlaceHolder)
                    return c4.Controls.Cast<Control>().OfType<T>();
                return Enumerable.Empty<T>();
            })
        .ToList();
    

    However note I’ve used is instead of a type comparison here. This is deliberate, because this is what the OfType LINQ method also uses internally.

    If you’re sure you want exact types rather than objects that pass an is comparison, you’ll have to implement your own OfType (or just use .Where(x => x.GetType == typeof(whatever)) instead.)

    (Also note that I’ve used Control instead of HtmlControl, in case some of your HtmlControls contain regular Controls.)

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

Sidebar

Related Questions

I have a method add which adds a generic object (i.e. T) into an
In asp.net mvc3 I want to have a generic method which adds a viewmodel
I have a generic Print method that iterates over a list and simply prints
I have a method which returns a generic type, is there a way to
I have constructed a generic method which deserialiazes an object in the following manner:
Why a generic method which constrains T to class would have boxing instructions in
I have a generic abstract class Factory<T> with a method createBoxedInstance() which returns instances
I have a generic method: (simplified) public class DataAccess : IDataAccess { public List<T>
I am usign EF 4 with repository patren which have a generic query method
I have a class Exporter which has a generic method which accepts an IEnumerable<T>

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.