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.

3287 questions
1151
votes
14 answers

Returning IEnumerable vs. IQueryable

What is the difference between returning IQueryable vs. IEnumerable, when should one be preferred over the other? IQueryable custs = from c in db.Customers where c.City == "" select c; IEnumerable custs = from c in…
stackoverflowuser
  • 20,984
  • 28
  • 65
  • 89
764
votes
22 answers

LINQ equivalent of foreach for IEnumerable

I'd like to do the equivalent of the following in LINQ, but I can't figure out how: IEnumerable items = GetItems(); items.ForEach(i => i.DoStuff()); What is the real syntax?
tags2k
  • 70,860
  • 30
  • 74
  • 105
741
votes
10 answers

IEnumerable vs List - What to Use? How do they work?

I have some doubts over how Enumerators work, and LINQ. Consider these two simple selects: List sel = (from animal in Animals join race in Species on animal.SpeciesKey equals race.SpeciesKey …
Axonn
  • 8,877
  • 5
  • 29
  • 39
446
votes
15 answers

How can I add an item to a IEnumerable collection?

My question as title above. For example, IEnumerable items = new T[]{new T("msg")}; items.ToList().Add(new T("msg2")); but after all it only has 1 item inside. Can we have a method like items.Add(item)? like the List
ldsenow
  • 5,257
  • 3
  • 17
  • 19
434
votes
12 answers

What is the difference between IQueryable and IEnumerable?

What is the difference between IQueryable and IEnumerable? See also What's the difference between IQueryable and IEnumerable that overlaps with this question.
Nirmal
408
votes
18 answers

Passing a single item as IEnumerable

Is there a common way to pass a single item of type T to a method which expects an IEnumerable parameter? Language is C#, framework version 2.0. Currently I am using a helper method (it's .Net 2.0, so I have a whole bunch of casting/projecting…
Groo
  • 45,930
  • 15
  • 109
  • 179
346
votes
6 answers

How can I return an empty IEnumerable?

Given the following code and the suggestions given in this question, I've decided to modify this original method and ask if there are any values in the IEnumarable return it, if not return an IEnumerable with no values. Here is the method: public…
Sergio Tapia
  • 36,466
  • 70
  • 175
  • 253
336
votes
19 answers

Count the items from a IEnumerable without iterating?

private IEnumerable Tables { get { yield return "Foo"; yield return "Bar"; } } Let's say I want iterate on those and write something like processing #n of #m. Is there a way I can find out the value of m without…
sebagomez
  • 9,029
  • 6
  • 48
  • 84
318
votes
8 answers

IEnumerable and Recursion using yield return

I have an IEnumerable method that I'm using to find controls in a WebForms page. The method is recursive and I'm having some problems returning the type I want when the yield return is returnig the value of the recursive call. My code looks as…
Jamie Dixon
  • 49,653
  • 18
  • 119
  • 157
300
votes
5 answers

Converting from IEnumerable to List

I want to convert from IEnumerable to List. How can I do this?
kartal
  • 14,786
  • 31
  • 93
  • 141
296
votes
16 answers

Can anyone explain IEnumerable and IEnumerator to me?

Can anyone explain IEnumerable and IEnumerator to me? For example, when to use it over foreach? what's the difference between IEnumerable and IEnumerator? Why do we need to use it?
prodev42
  • 5,821
  • 7
  • 30
  • 34
256
votes
7 answers

Shorter syntax for casting from a List to a List?

I know it's possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List ListOfY = new List(); foreach(X x in ListOfX) …
Jimbo
  • 20,449
  • 37
  • 111
  • 151
232
votes
4 answers

How to concatenate two IEnumerable into a new IEnumerable?

I have two instances of IEnumerable (with the same T). I want a new instance of IEnumerable which is the concatenation of both. Is there a built-in method in .NET to do that or do I have to write it myself?
Samuel Rossille
  • 16,848
  • 18
  • 55
  • 85
194
votes
2 answers

Recreating a Dictionary from an IEnumerable>

I have a method that returns an IEnumerable>, but some of the callers require the result of the method to be a dictionary. How can I convert the IEnumerable> into a Dictionary
learnerplates
  • 3,769
  • 5
  • 23
  • 39
174
votes
6 answers

Return all enumerables with yield return at once; without looping through

I have the following function to get validation errors for a card. My question relates to dealing with GetErrors. Both methods have the same return type IEnumerable. private static IEnumerable GetErrors(Card card) { var…
John Oxley
  • 13,918
  • 17
  • 49
  • 76
1
2 3
99 100