Questions tagged [ienumerable]

IEnumerable, and its generic counterpart IEnumerable are .NET interfaces for iterating (or enumerating) through a collection of items.

The IEnumerable interface (in the System.Collections namespace) contains only a single member, the GetEnumerator() method, which returns an IEnumerator.

The generic counterpart to IEnumerable, IEnumerable<T> (added to the System.Collections.Generic namespace in the .NET framework 2.0) also contains only a single member, the GetEnumerator() method; this returns an IEnumerator<T>.

Interaction with the IEnumerable<T> interface is often done through extension methods, for example First(), Last() and Count().

It is possible to obtain a generic IEnumerable<T> from an IEnumerable by calling either of the extension methods Cast<T>(), or OfType<T>(), depending on the case.

3522 questions
53
votes
8 answers

Convert DataTable to IEnumerable

I am trying to convert a DataTable to an IEnumerable. Where T is a custom type I created. I know I can do it by creating a List but I was thinking if there is a slicker way to do it using IEnumerable. Here is what I have now: private…
mpenrow
  • 5,563
  • 9
  • 30
  • 36
52
votes
2 answers

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…
Simsons
  • 12,295
  • 42
  • 153
  • 269
51
votes
2 answers

Why do arrays in .net only implement IEnumerable and not IEnumerable?

I was implementing my own ArrayList class and was left surprised when I realised that public System.Collections.Generic.IEnumerator GetEnumerator() { return _array.GetEnumerator(); } didn't work. What is the reason arrays don't implement…
devoured elysium
  • 101,373
  • 131
  • 340
  • 557
50
votes
4 answers

Is there a standard C++ equivalent of IEnumerable in C#?

Or is it safe to use vector if the Enumerator of T is just listing all the elements?
derekhh
  • 5,272
  • 11
  • 40
  • 60
49
votes
4 answers

Why doesn't the Controls collection provide all of the IEnumerable methods?

I'm not for sure how the ControlCollection of ASP.Net works, so maybe someone can shed some light on this for me. I recently discovered the magic that is extension methods and Linq. Well, I was very sad to find that this isn't valid syntax var…
Earlz
  • 62,085
  • 98
  • 303
  • 499
49
votes
5 answers

With Entity Framework is it better to use .First() or .Take(1) for "TOP 1"?

We are implementing some EF data repositories, and we have some queries which would include TOP 1 I have read many posts suggesting to use .Take(1) The code I'm reviewing uses .First() I understand that both of these produce the same result for…
Matthew
  • 10,244
  • 5
  • 49
  • 104
48
votes
6 answers

Recommended way to check if a sequence is empty

A method returns a sequence, IEnumerable, and you now want to check if it is empty. How do you recommend doing that? I'm looking for both good readability and good performance. The first and most obvious way is to check that the count is greater…
Svish
  • 152,914
  • 173
  • 462
  • 620
48
votes
4 answers

IEnumerable and order

I have got a question about the order in IEnumerable. As far as I am aware, iterating through IEnumerable is pseudo-code can be written in the following way: while (enumerable.HasNext()) { object obj = enumerable.Current; ... } Now, assume,…
undefined
  • 1,354
  • 1
  • 8
  • 19
47
votes
4 answers

Appending/concatenating two IEnumerable sequences

I have two sets of datarows. They are each IEnumerable. I want to append/concatenate these two lists into one list. I'm sure this is doable. I don't want to do a for loop and noticed that there is a Union method and a Join method on the two Lists.…
MikeTWebb
  • 9,149
  • 25
  • 93
  • 132
47
votes
5 answers

How to access index in IEnumerable object in C#?

I have an IEnumerable object. I would like to access based on index for instance: for(i=0; i<=Model.Products; i++) { ??? } Is this possible?
dcpartners
  • 5,176
  • 13
  • 50
  • 73
47
votes
8 answers

Optimal LINQ query to get a random sub collection - Shuffle

Please suggest an easiest way to get a random shuffled collection of count 'n' from a collection having 'N' items. where n <= N
Jobi Joy
  • 49,102
  • 20
  • 108
  • 119
46
votes
4 answers

Remove items from IEnumerable

I have 2 IEnumerable collections. IEnumerable objectsToExcept and IEnumerable allObjects. objectsToExcept may contain objects from allObjects. I need to remove from allObjects objects in objectsToExcept. For example: foreach…
Maxim
  • 1,555
  • 5
  • 18
  • 28
44
votes
2 answers

Flatten IEnumerable>; understanding generics

I wrote this extension method (which compiles): public static IEnumerable Flatten(this IEnumerable @this) where T : IEnumerable { foreach (T t in @this) foreach (J j in t) …
Daryl
  • 3,253
  • 4
  • 29
  • 39
44
votes
3 answers

Why was IEnumerable made covariant in C# 4?

In earlier versions of C# IEnumerable was defined like this: public interface IEnumerable : IEnumerable Since C# 4 the definition is: public interface IEnumerable : IEnumerable Is it just to make the annoying casts in LINQ expressions…
soc
  • 27,983
  • 20
  • 111
  • 215
43
votes
2 answers

Why Enumerable.Cast raises an InvalidCastException?

If I can implicitly cast an integer value to a double, like: int a = 4; double b = a; // now b holds 4.0 Why can I not do this: int[] intNumbers = {10, 6, 1, 9}; double[] doubleNumbers2 = intNumbers.Cast().ToArray(); I get a…
outlookrperson
  • 2,761
  • 7
  • 32
  • 49