{"id":122,"date":"2011-03-23T20:01:00","date_gmt":"2011-03-24T00:01:00","guid":{"rendered":"http:\/\/madprogrammer76.wordpress.com\/2011\/03\/24\/using-linq-to-find-a-control-in-the-controlcollection"},"modified":"2013-12-15T21:20:39","modified_gmt":"2013-12-16T02:20:39","slug":"using-linq-to-find-a-control-in-the-controlcollection","status":"publish","type":"post","link":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/","title":{"rendered":"Using LINQ to find a control in the ControlCollection"},"content":{"rendered":"<p>While writing a windows application in C#, I realized that there was no find control method in the ControlCollection class.   This was a problem as it would create a significant amount of code to find a control.  Well it turns out you can do it using LINQ.   I had found a blog that I was able to implement the code fairly cleanly.  The original code limited me a little bit, but with a very minor modification I came up with this code. <\/p>\n<p><b>Original Blog:<\/b> <a href=\"http:\/\/weblogs.asp.net\/dfindley\/archive\/2007\/06\/29\/linq-the-uber-findcontrol.aspx\">linq-the-uber-findcontrol<\/a><\/p>\n<pre style=\"background-color:#ffffcc;font-size:1.1em;\">public static class PageExtensions<br \/>    {<br \/>        public static IEnumerable All(this Control.ControlCollection controls)<br \/>        {<br \/>            foreach (Control control in controls)<br \/>            {<br \/>                if (control.HasChildren)<br \/>                {<br \/>                    foreach (Control child in control.Controls.All()) <br \/>                    {<br \/>                         yield return child;<br \/>                    }<br \/>                }<br \/>                else<br \/>                {<br \/>                    yield return control;<br \/>                }<br \/>            }<br \/>        }<br \/>    }<br \/><br \/>     private Control GetControlByName(string name)<br \/>     {<br \/>            Control firstEmpty = this.Controls.All()<br \/>            .OfType()<br \/>            .Where(tb =&gt; tb.Name.Trim().Equals(name))<br \/>            .FirstOrDefault();<br \/>            return firstEmpty;<br \/>     }<br \/><\/pre>\n<div class=\"blogger-post-footer\"><img width='1' height='1' src='https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com' alt='' \/><\/div>\n","protected":false},"excerpt":{"rendered":"<p>While writing a windows application in C#, I realized that there was no find control method in the ControlCollection class. This was a problem as it would create a significant amount of code to find a control. Well it turns out you can do it using LINQ. I had found a blog that I was &hellip; <a href=\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Using LINQ to find a control in the ControlCollection<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,13,32,48],"tags":[180,179,178],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v23.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\r\n<title>Using LINQ to find a control in the ControlCollection - LDNDeveloper<\/title>\r\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\" \/>\r\n<meta property=\"og:locale\" content=\"en_US\" \/>\r\n<meta property=\"og:type\" content=\"article\" \/>\r\n<meta property=\"og:title\" content=\"Using LINQ to find a control in the ControlCollection - LDNDeveloper\" \/>\r\n<meta property=\"og:description\" content=\"While writing a windows application in C#, I realized that there was no find control method in the ControlCollection class. This was a problem as it would create a significant amount of code to find a control. Well it turns out you can do it using LINQ. I had found a blog that I was &hellip; Continue reading Using LINQ to find a control in the ControlCollection\" \/>\r\n<meta property=\"og:url\" content=\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\" \/>\r\n<meta property=\"og:site_name\" content=\"LDNDeveloper\" \/>\r\n<meta property=\"article:published_time\" content=\"2011-03-24T00:01:00+00:00\" \/>\r\n<meta property=\"article:modified_time\" content=\"2013-12-16T02:20:39+00:00\" \/>\r\n<meta property=\"og:image\" content=\"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com\" \/>\r\n<meta name=\"author\" content=\"andrewpallant\" \/>\r\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\r\n<meta name=\"twitter:creator\" content=\"@ldnDeveloper\" \/>\r\n<meta name=\"twitter:site\" content=\"@LdnDeveloper\" \/>\r\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"andrewpallant\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\r\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\"},\"author\":{\"name\":\"andrewpallant\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/0e7b5e71751000e8f66b17b69ef4ab97\"},\"headline\":\"Using LINQ to find a control in the ControlCollection\",\"datePublished\":\"2011-03-24T00:01:00+00:00\",\"dateModified\":\"2013-12-16T02:20:39+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\"},\"wordCount\":94,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"image\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com\",\"keywords\":[\"c#\",\"controls\",\"linq\"],\"articleSection\":[\"Better Coding\",\"C#\",\"Finding Controls\",\"LINQ\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\",\"name\":\"Using LINQ to find a control in the ControlCollection - LDNDeveloper\",\"isPartOf\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com\",\"datePublished\":\"2011-03-24T00:01:00+00:00\",\"dateModified\":\"2013-12-16T02:20:39+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage\",\"url\":\"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com\",\"contentUrl\":\"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/andrewpallant.ca\/wordpress\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Using LINQ to find a control in the ControlCollection\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#website\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/\",\"name\":\"LDNDeveloper\",\"description\":\"Learning, Growing and Sharing.\",\"publisher\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84\",\"name\":\"ldnDeveloper\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"contentUrl\":\"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg\",\"width\":512,\"height\":512,\"caption\":\"ldnDeveloper\"},\"logo\":{\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\"},\"description\":\"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf\",\"sameAs\":[\"http:\/\/www.andrewpallant.ca\",\"https:\/\/x.com\/LdnDeveloper\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/0e7b5e71751000e8f66b17b69ef4ab97\",\"name\":\"andrewpallant\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g\",\"caption\":\"andrewpallant\"},\"url\":\"https:\/\/andrewpallant.ca\/wordpress\/author\/andrewpallant\/\"}]}<\/script>\r\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Using LINQ to find a control in the ControlCollection - LDNDeveloper","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/","og_locale":"en_US","og_type":"article","og_title":"Using LINQ to find a control in the ControlCollection - LDNDeveloper","og_description":"While writing a windows application in C#, I realized that there was no find control method in the ControlCollection class. This was a problem as it would create a significant amount of code to find a control. Well it turns out you can do it using LINQ. I had found a blog that I was &hellip; Continue reading Using LINQ to find a control in the ControlCollection","og_url":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/","og_site_name":"LDNDeveloper","article_published_time":"2011-03-24T00:01:00+00:00","article_modified_time":"2013-12-16T02:20:39+00:00","og_image":[{"url":"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com"}],"author":"andrewpallant","twitter_card":"summary_large_image","twitter_creator":"@ldnDeveloper","twitter_site":"@LdnDeveloper","twitter_misc":{"Written by":"andrewpallant","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#article","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/"},"author":{"name":"andrewpallant","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/0e7b5e71751000e8f66b17b69ef4ab97"},"headline":"Using LINQ to find a control in the ControlCollection","datePublished":"2011-03-24T00:01:00+00:00","dateModified":"2013-12-16T02:20:39+00:00","mainEntityOfPage":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/"},"wordCount":94,"commentCount":0,"publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"image":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage"},"thumbnailUrl":"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com","keywords":["c#","controls","linq"],"articleSection":["Better Coding","C#","Finding Controls","LINQ"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/","url":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/","name":"Using LINQ to find a control in the ControlCollection - LDNDeveloper","isPartOf":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#website"},"primaryImageOfPage":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage"},"image":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage"},"thumbnailUrl":"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com","datePublished":"2011-03-24T00:01:00+00:00","dateModified":"2013-12-16T02:20:39+00:00","breadcrumb":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#primaryimage","url":"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com","contentUrl":"https:\/\/blogger.googleusercontent.com\/tracker\/4951456985313329413-7414052714129705981?l=softwaredeveloperinlondon.blogspot.com"},{"@type":"BreadcrumbList","@id":"https:\/\/andrewpallant.ca\/wordpress\/using-linq-to-find-a-control-in-the-controlcollection\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/andrewpallant.ca\/wordpress\/"},{"@type":"ListItem","position":2,"name":"Using LINQ to find a control in the ControlCollection"}]},{"@type":"WebSite","@id":"https:\/\/andrewpallant.ca\/wordpress\/#website","url":"https:\/\/andrewpallant.ca\/wordpress\/","name":"LDNDeveloper","description":"Learning, Growing and Sharing.","publisher":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/andrewpallant.ca\/wordpress\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/f6f5bb1ac3e0c5a54a8b5ce35fd67b84","name":"ldnDeveloper","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","contentUrl":"https:\/\/andrewpallant.ca\/wordpress\/wp-content\/uploads\/2017\/05\/cropped-AAEAAQAAAAAAAAXQAAAAJDQxMGRlMzFjLWM4ODctNDk1NC05M2EyLWE1NDNhNTRiZjVlYw-2.jpg","width":512,"height":512,"caption":"ldnDeveloper"},"logo":{"@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/"},"description":"Andrew Pallant (@LdnDeveloper) has been a web, database and desktop developer for over 16 years. Andrew has worked on projects that ranged from factory automation to writing business applications. Most recently he has been heavily involved in various forms for ecommerce projects. Over the years Andrew has worn many hats: Project Manager, IT Manager, Lead Developer, Supervisor of Developers and many more - See more at: http:\/\/www.unlatched.com\/#sthash.8DiTkpKy.dpuf","sameAs":["http:\/\/www.andrewpallant.ca","https:\/\/x.com\/LdnDeveloper"]},{"@type":"Person","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/0e7b5e71751000e8f66b17b69ef4ab97","name":"andrewpallant","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/andrewpallant.ca\/wordpress\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","caption":"andrewpallant"},"url":"https:\/\/andrewpallant.ca\/wordpress\/author\/andrewpallant\/"}]}},"_links":{"self":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/122"}],"collection":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/comments?post=122"}],"version-history":[{"count":1,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/122\/revisions"}],"predecessor-version":[{"id":1130,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/posts\/122\/revisions\/1130"}],"wp:attachment":[{"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/media?parent=122"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/categories?post=122"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andrewpallant.ca\/wordpress\/wp-json\/wp\/v2\/tags?post=122"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}