score:92
no, ienumerable
doesn't have many extension methods on it: ienumerable<t>
does. they are two separate interfaces, although ienumerable<t>
extends ienumerable
.
the normal linq ways of converting are to use the cast<t>()
and oftype<t>()
extension methods which do extend the nongeneric interface:
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.
score:2
linq utilized generic collections. controlscollection implements ienumerable
not ienumberable<t>
if you notice this will not work
((ienumerable)page.controls).where(...
however, this does
((ienumerable<control>)page.controls).where(...
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();
score:5
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();
score:11
this is just because the controlcollection
class came around before generics; so it implements ienumerable
but not ienumerable<control>
.
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();
Source: stackoverflow.com
Related Query
- 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
- Convert LINQ query to php mongodb query format
- Linq where clause "if item is not null, evaluate, otherwise, get all data"
- Add/Update IEnumerable List
- .NET Dynamic Search Function
- query a sub-collection of a collection with linq
- Unable to cast string type to number type in Linq expression
- How to compare two datatables and only check for the rows found
- LINQ select depending on inner collection value
- How to translate this Queryable linq function
- Execute method for each object in generic list using lambda
- db4o, Linq, and UUID's
- Sort a List by the column of a different list
- LINQ: From a list of type T, retrieve only objects of a certain subclass S
- ASP.Netcore MVC - Razor Displaying expense totals for categories by Month/Year rows and total expenses per month using efcore
- CopyToDataTable in LINQ
- Query using EF "Include" and including the related tables on Where
- LINQ to XML to read Hierarchal XML Document
- Unable to create non-generic LINQ function within Repository class
- Reuse stored procedure result in linq to sql in Code First
- C# Sorting alphabetical order a - z and then to aa, ab - zz
- Selecting specific columns using linq: What gets transferred?
- LINQ Query with both CASE statement and SUM function
- Linq statement to select common elements between two collections
- Linq Where Clause with Into
- What is the quickest way to group by and get the MAX of something in c#?
- Nhibernate 3 Linq - inner joins
- Set all array values to 0 with Lambda/linq
- Accessing values of XML using Linq
- MVC 4 how pass data correctly from controller to view
- Call Method For Rand Number Generation While Selecting Data With Linq