Click here to Skip to main content
12,634,101 members (27,478 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

20.3K views
9 bookmarked
Posted

Cast an IEnumerable to an IEnumerable(T)

, 22 Dec 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
In this tip, I tell you how to cast an IEnumerable to an IEnumerable(T)

Introduction

It's possible to use LINQ queries to get data from an IEnumerable<T>. But for an IEnumerable, you can't use LINQ. If you write this LINQ query for anIEnumerable :

IEnumerable ienumerable = new int[] { 1, 5, 6, 7, 8, 11, 10, 134, 90 };
var integer = from i in ienumerable where i > 10 select i;

Then, you get this error:

Could not find an implementation of the query pattern for 
source type 'System.Collections.IEnumerable'. 'Where' not found. 
Consider explicitly specifying the type of the range variable 'i'. 

In this tip, I tell you how to cast an IEnumerable to an IEnumerable<T>.

Cast an IEnumerable to an IEnumerable<T>

To cast anIEnumerable to anIEnumerable<T> , you can use this code:

IEnumerable ienumerable = new int[] { 1, 5, 6, 7, 8, 11, 10, 134, 90 };
IEnumerable casted = ienumerable.Cast<int>(); // change 'int' into the 
                              //type of the elements in your IEnumerable

Now, you can use a LINQ query for casted. But, an IEnumerable can contain different types:

IEnumerable ienumerable = new object[] { 1, 5, 6, 7, 8, 11, 10, 134, 90, "test" };

With this code, you can get all integers from the IEnumerable into a IEnumerable<T> using the OfType<T> function:

IEnumerable ienumerable = new object[] { 1, 5, 6, 7, 8, 11, 10, 134, 90, "test" };
IEnumerable<int> allIntegers = ienumerable.OfType<int>(); // change 'int' 
      // in the type of the elements you want to get from the IEnumerable

Points of Interest

There're many collection classes in C# that inherit from IEnumerable, for example System.Windows.Forms.Control.ControlCollection, System.Windows.Forms.HtmlElementCollection or System.Xml.XmlNodeList class. It's useful to use LINQ for these classes.

History

  • 22 Dec 2012: First version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

ProgramFOX
Belgium Belgium
I'm a hobbyist programmer. My favourite languages are C#, JavaScript, and Python. I also like chess; you can find me on Lichess.

You may also be interested in...

Comments and Discussions

 
GeneralMy vote of 5 Pin
Tarek Elqusi23-Dec-12 6:27
memberTarek Elqusi23-Dec-12 6:27 
GeneralRe: My vote of 5 Pin
ProgramFOX23-Dec-12 23:51
memberProgramFOX23-Dec-12 23:51 
Questiongood Pin
UllasN22-Dec-12 21:23
memberUllasN22-Dec-12 21:23 
AnswerRe: good Pin
ProgramFOX22-Dec-12 22:46
memberProgramFOX22-Dec-12 22:46 
GeneralRe: good Pin
UllasN23-Dec-12 0:12
memberUllasN23-Dec-12 0:12 
Please keep posting small articles . Probably too much to ask of anyone but these small articles are the best as they are like little gems of knowledge that can read right off an RSS feed in a mobile device . Quick knowledge on the go keeps the brain ticking.

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.161208.2 | Last Updated 22 Dec 2012
Article Copyright 2012 by ProgramFOX
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid