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
119
votes
5 answers

How to loop through a collection that supports IEnumerable?

How to loop through a collection that supports IEnumerable?
mrblah
  • 99,669
  • 140
  • 310
  • 420
116
votes
4 answers

How to sort an IEnumerable

How can I sort an IEnumerable alphabetically. Is this possible? Edit: How would I write an in-place solution?
CatZilla
  • 1,456
  • 3
  • 12
  • 13
112
votes
5 answers

IEnumerable vs IReadonlyCollection vs ReadonlyCollection for exposing a list member

I have spent quite a few hours pondering the subject of exposing list members. In a similar question to mine, Jon Skeet gave an excellent answer. Please feel free to have a look. ReadOnlyCollection or IEnumerable for exposing member collections? I…
Marcel De Villiers
  • 1,682
  • 4
  • 15
  • 17
109
votes
2 answers

Convert IAsyncEnumerable to List

So in C#8 we got the addition of the IAsyncEnumerable interface. If we have a normal IEnumerable we can make a List or pretty much any other collection we want out of it. Thanks to Linq there. var range = Enumerable.Range(0, 100); var list =…
Twenty
  • 5,234
  • 4
  • 32
  • 67
107
votes
9 answers

How to loop through IEnumerable in batches

I am developing a C# program which has an "IEnumerable users" that stores the ids of 4 million users. I need to loop through the IEnumerable and extract a batch 1000 ids each time to perform some operations in another method. How do I extract 1000…
user1526912
  • 15,818
  • 14
  • 57
  • 92
106
votes
6 answers

Freely convert between List and IEnumerable

How can I convert a List to an IEnumerable and then back again? I want to do this in order to run a series of LINQ statements on the List, e. g. Sort()
TK.
  • 46,577
  • 46
  • 119
  • 147
104
votes
5 answers

Is it possible to do start iterating from an element other than the first using foreach?

I'm thinking about implementing IEnumerable for my custom collection (a tree) so I can use foreach to traverse my tree. However as far as I know foreach always starts from the first element of the collection. I would like to choose from which…
inferno
  • 1,221
  • 3
  • 9
  • 6
100
votes
7 answers

Is there an "Empty List" singleton in C#?

In C# I use LINQ and IEnumerable a good bit. And all is well-and-good (or at least mostly so). However, in many cases I find myself that I need an empty IEnumerable as a default. That is, I would like for (var x in xs) { ... } to work without…
user166390
100
votes
3 answers

Does LINQ work with IEnumerable?

I have a class that implements IEnumerable, but doesn't implement IEnumerable. I can't change this class, and I can't use another class instead of it. As I've understood from MSDN LINQ can be used if class implements IEnumerable. I've tried…
Bogdan Verbenets
  • 25,686
  • 13
  • 66
  • 119
100
votes
10 answers

Cannot apply indexing with [] to an expression of type 'System.Collections.Generic.IEnumerable<>

Is there any specific reason why indexing is not allowed in IEnumerable. I found a workaround for the problem I had, but just curious to know why it does not allow indexing. Thanks,
Sandeep
  • 7,156
  • 12
  • 45
  • 57
97
votes
5 answers

Map two lists into a dictionary in C#

Given two IEnumerables of the same size, how can I convert it to a Dictionary using Linq? IEnumerable keys = new List() { "A", "B", "C" }; IEnumerable values = new List() { "Val A", "Val B", "Val C" }; var dictionary…
BrunoLM
  • 97,872
  • 84
  • 296
  • 452
96
votes
6 answers

How to convert IEnumerable to one comma separated string?

Say that for debugging purposes, I want to quickly get the contents of an IEnumerable into one-line string with each string item comma-separated. I can do it in a helper method with a foreach loop, but that's neither fun nor brief. Can Linq be used?…
Johann Gerell
  • 24,991
  • 10
  • 72
  • 122
94
votes
11 answers

Is there a Linq method to add a single item to an IEnumerable?

I am trying to do something like this: image.Layers which returns an IEnumerable for all layers except the Parent layer, but in some cases, I just want to do: image.Layers.With(image.ParentLayer); because it's only used in a few places…
Joan Venge
  • 315,713
  • 212
  • 479
  • 689
88
votes
8 answers

JavaScript/jQuery equivalent of LINQ Any()

Is there an equivalent of IEnumerable.Any(Predicate) in JavaScript or jQuery? I am validating a list of items, and want to break early if error is detected. I could do it using $.each, but I need to use an external flag to see if the item was…
dilbert
  • 881
  • 1
  • 6
  • 3
87
votes
10 answers

There is no ViewData item of type 'IEnumerable' that has the key 'xxx'

There are a couple of posts about this on Stack Overflow but none with an answer that seem to fix the problem in my current situation. I have a page with a table in it, each row has a number of text fields and a dropdown. All the dropdowns need to…
Jimbo
  • 22,379
  • 42
  • 117
  • 159