IEnumerable doesn't have many extension methods on it:
IEnumerable<T> does. They are two separate interfaces, although
IEnumerable<TextBox> textBoxes = Controls.OfType<TextBox>(); IEnumerable<Control> controls = Controls.Cast<Control>();
The difference between the two is that
OfType will just skip any items which aren't of the required type;
Cast will throw an exception instead.
Once you've got references to the generic
IEnumerable<T> type, all the rest of the LINQ methods are available.
Linq utilized Generic Collections. ControlsCollection implements
If you notice this will not work
However, this does
You can either cast to Generic
IEnumerable<T> or access an extension method that does, like so:
page.Controls.OfType<Control>().Where(c => c.ID == "Some ID").FirstOrDefault();
In addition to the answers provided by Jon Skeet and Dan Tao, you can use query expression syntax by explicitly providing the type.
Control myControl = (from Control control in this.Controls where control.ID == "Some ID" select control).SingleOrDefault();
This is just because the
ControlCollection class came around before generics; so it implements
IEnumerable but not
Fortunately, there does exist a LINQ extension method on the
IEnumerable interface that allows you to generate an
IEnumerable<T> through casting:
Cast<T>. Which means you can always just do this:
var c = Controls.Cast<Control>().Where(x => x.ID == "Some ID").SingleOrDefault();
- Why doesn't the Controls collection provide all of the IEnumerable methods?
- Why is IQueryable lacking all the extension methods that IQueryable<T> has?
- Why are LINQ operators defined as extension methods on IEnumerable interface and are not part of the interface itself? C#
- What is the best way to find all dependent children in a IEnumerable collection
- How to perform .Max() on a property of all objects in a collection and return the object with maximum value
- Why don't the Linq extension methods sit on IEnumerator rather than IEnumerable?
- Why the order of LINQ to objects methods counts
- Check if all items in a Collection have the same value
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- How to join together all the elements in an IEnumerable of IEnumerables?
- XElement: a collection of all the leaves?
- Why is Entity Framework's AsEnumerable() downloading all data from the server?
- Find rows where child collection contains all elements of the list
- Why not reveal the type and identity of the source to the client?
- Does casting an IEnumerable to an IList<TResult> enumerate the sequence, and why would this be preferred?
- Using Linq to loop through all controls only get the first control
- Why doesn't the catch statement catch all the exceptions?
- Obtain the Sum of an IEnumerable collection in only one LINQ expression
- Why does LINQ treat two methods that do the "same" thing differently?
- Linq Any returns true despite all values in the collection being false
- Why EF can't sort items by the order of another collection and how to workaround?
- Why did my code work when I changed it from IEnumerable to List?
- Linq methods not available for a collection implementing IEnumerable
- Why are stored procedures, functions, and views put into a .dbml file instead of the code file?
- Can I use a LINQ IEnumerable result as the data source for a Gtk.TreeView?
- If I have a collection of classes how can I return a collection of a single attribute of all the classes?
- Why the extension method of where for LINQ in this code would print out a single number while it shouldn't print anything at all?
- why are lambdas & anonymous methods not allowed on the left side of the is or as operator?
- IEnumerable extension methods (System.Linq) unavailable when inheriting from collection and implementing enumerable interfaces
- Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID
More Query from same tag
- How should I compare values in two lists?
- Need to join two strings if both contain values, or return one value if first is NULL
- Unknown exception when updating values through entity framework?
- How to get index of string found in list and equal to input string
- Linq Expression Decimal Contains method
- LINQ return items in a List that matches any Names (string) in another list
- How to create response from dictionary?
- Second "from" statement in LINQ
- Find next record in a set: LINQ
- Retrieve and print data from dynamic sql query with linq
- How to cross join deserialization json data in C#
- Compare Linq Query Result to Array
- LINQ : select all that match a single field, merge on original
- How can I simplify Linq extension method invokation using reflection?
- LINQ is able to filter on association, but returns 0 results when referenced directly
- Ordering list by many conditions using Linq
- Recursive linq query to get parent list or child list from the given id
- Convert SQL query to linq lambda Entity Framework Core
- C# Lambda Expressions Using Complex Objects
- Linq check if value exists otherwise set default value
- ASP.NET MVC How to use User's data after they log in
- How to work with nested Arrays and Objects using LINQ
- How do you get the sum of the product of two properties in an IEnumerable using LINQ?
- Read data from string XML with node enabled false
- Is there a way in C# to use EXCEPT to compare 2 lists when one doesn't have Id yet?
- Dynamic Linq SearchFor from entities by Passing Tablename,ColumnName and value as parameter
- Best way to get all words like @*@ from a string in C#
- How can I combine this code into one or two LINQ queries?
- Taking out items from XML C# and performing bulk delete
- Purpose of EF 6.x DbContext Generator option when adding a new data item in Visual Studio