Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to go to particular Item in IEnumerable

I have IEnumerable which contains number Data inside it.

Edit The IEnumerable is from System.Collection.Ienumerable directive.

Attached the snapShot of Viual Studio, Enum that Contains Data:

alt text http://www.freeimagehosting.net/uploads/bd72c6c310.jpg

Just to brief about the above image, eLevelData is the IEnumerable variable, in which I have my data .

Now I want to go to the data at index 4 or 5, but I don't want to use foreach loop. Any suggestions please.

Thanks,

Subhen

like image 651
Simsons Avatar asked May 26 '10 10:05

Simsons


People also ask

How do I access IEnumerable?

We can get first item values from IEnumerable list by using First() property or loop through the list to get respective element. IEnumerable list is a base for all collections and its having ability to loop through the collection by using current property, MoveNext and Reset methods in c#, vb.net.

What does I mean in IEnumerable?

IEnumerable is an interface defining a single method GetEnumerator() that returns an IEnumerator interface. It is the base interface for all non-generic collections that can be enumerated. This works for read-only access to a collection that implements that IEnumerable can be used with a foreach statement.


2 Answers

var item = eLevelData.ElementAt(index);

If your collection is typed as IEnumerable instead of IEnumerable<T> you'll need to use the Cast extension method before you can call ElementAt e.g.

var item = eLevelData.Cast<RMSRequestProcessor.RMSMedia>().ElementAt(index)

like image 152
Lee Avatar answered Sep 30 '22 12:09

Lee


Don't know much about what subset of .NET BCL/LINQ is available in Silverlight, but Skip should do the trick. But generally speaking it still uses foreach internally:

var item = eLevelData.Skip(4 /* or 5 */).First(); 
like image 44
Anton Gogolev Avatar answered Sep 30 '22 11:09

Anton Gogolev