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
20
votes
3 answers

Chaining IEnumerables in C#?

Is there a simple built-in way to take an ordered list of IEnumerables and return a single IEnumerable which yields, in order, all the elements in the first, then the second, and so on. I could certainly write my own, but I wanted to know whether…
recursive
  • 83,943
  • 34
  • 151
  • 241
20
votes
6 answers

Can IEnumerable.Select() skip an item?

I have this function: public IEnumerable EnumPrograms() { return dev.AudioSessionManager2.Sessions.AsEnumerable() .Where(s => s.GetProcessID != 0) .Select(s => { try { return…
ThiefMaster
  • 310,957
  • 84
  • 592
  • 636
20
votes
3 answers

Why ASP.NET MVC default Model Binder is slow? It's taking a long time to do its work

In a current project the client asked for the possibility of answering a questionnaire in two ways: using a Wizard (one question at a time) and Listing (all questions at once) in a single form. Both ways are already implemented. The questions are…
Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
20
votes
2 answers

Assert.AreEqual does not use my .Equals overrides on an IEnumerable implementation

I have a PagedModel class which implements IEnumerable to just return the ModelData, ignoring the paging data. I have also overridden Equals and GetHashCode to allow comparing two PagedModel objects by their ModelData, PageNumber, and TotalPages,…
just.another.programmer
  • 8,579
  • 8
  • 51
  • 90
19
votes
3 answers

Bind Checkboxes to int array/enumerable in MVC

@Html.CheckBox("orderNumbers", new { value = 1 }) @Html.CheckBox("orderNumbers", new { value = 2 }) @Html.CheckBox("orderNumbers", new { value = 3 }) @Html.CheckBox("orderNumbers", new { value = 4 }) @Html.CheckBox("orderNumbers", new { value = 5…
fearofawhackplanet
  • 52,166
  • 53
  • 160
  • 253
19
votes
5 answers

Getting head and tail from IEnumerable that can only be iterated once

I have a sequence of elements. The sequence can only be iterated once and can be "infinite". What is the best way get the head and the tail of such a sequence? Update: A few clarifications that would have been nice if I included in the original…
asgerhallas
  • 16,890
  • 6
  • 50
  • 68
19
votes
3 answers

How can I create a singleton IEnumerable?

Does C# offer some nice method to cast a single entity of type T to IEnumerable? The only way I can think of is something like: T entity = new T(); IEnumerable = new List { entity }.AsEnumerable(); And I guess there should be a better way.
Yippie-Ki-Yay
  • 22,026
  • 26
  • 90
  • 148
19
votes
3 answers

IEnumerable> to IEnumerable using LINQ

How to split an IEnumerable of IEnumerables to one flat IEnumerable using LINQ (or someway else)?
abatishchev
  • 98,240
  • 88
  • 296
  • 433
19
votes
3 answers

Check if IEnumerable has ANY rows without enumerating over the entire list

I have the following method which returns an IEnumerable of type T. The implementation of the method is not important, apart from the yield return to lazy load the IEnumerable. This is necessary as the result could have millions of items. public…
Dave New
  • 38,496
  • 59
  • 215
  • 394
19
votes
8 answers

Is the order of objects returned by FOREACH stable?

Is it safe to assume that two itterations over the same collection will return the objects in the same order? Obviously, it is assumed that the collection has not otherwise been changed.
Metro
  • 1,464
  • 14
  • 24
18
votes
6 answers

Is there an equivalent to Python's enumerate() for .NET IEnumerable

I could not find a related question. In python you can easily loop through a sequence (list, generator etc) and collect the index of the iteration at the same time thanks to enumerate(seq) like this : >>> for (i,item) in…
tsimbalar
  • 5,790
  • 6
  • 37
  • 61
18
votes
5 answers

Determine if an IEnumerable contains any object of another IEnumerable

I have 2 IEnumerable IEnumerable x; IEnumerable y; What is the best best way to determine if any int in y is present in the x? Currently I'm using: return x.Intersect(y).Count() > 0; Would it be significantly faster to loop…
Adam
  • 1,984
  • 2
  • 16
  • 19
18
votes
9 answers

Check IEnumerable for items having duplicate properties

How to check if an IEnumerable has two or more items with the same property value ? For example a class public class Item { public int Prop1 {get;set;} public string Prop2 {get;set;} } and then a collection of type IEnumerable I need…
user137348
  • 10,166
  • 18
  • 69
  • 89
18
votes
8 answers

How to make IEnumerable readonly?

Why are the lists list1Instance and p in the Main method of the below code pointing to the same collection? class Person { public string FirstName = string.Empty; public string LastName = string.Empty; public…
gk.
  • 1,252
  • 3
  • 14
  • 23
18
votes
2 answers

Why is the compiler-generated enumerator for "yield" not a struct?

The compiler-generated implementation of IEnumerator / IEnumerable for yield methods and getters seems to be a class, and is therefore allocated on the heap. However, other .NET types such as List specifically return struct enumerators to avoid…
Lazlo
  • 8,518
  • 14
  • 77
  • 116