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

Is Yield Return == IEnumerable & IEnumerator?

Is yield return a shortcut for implementing IEnumerable and IEnumerator?
John
  • 5,381
  • 4
  • 30
  • 26
33
votes
6 answers

What's the Best Way to Add One Item to an IEnumerable?

Here's how I would add one item to an IEnumerable object: //Some IEnumerable object IEnumerable arr = new string[] { "ABC", "DEF", "GHI" }; //Add one item arr = arr.Concat(new string[] { "JKL" }); This is awkward. I don't see a method…
user2023861
  • 8,030
  • 9
  • 57
  • 86
32
votes
5 answers

Creating the IEnumerable> Objects with C#?

For testing purposes, I need to create an IEnumerable> object with the following sample key value pairs: Key = Name | Value : John Key = City | Value : NY What is the easiest approach to do this?
Chathuranga Chandrasekara
  • 20,548
  • 30
  • 97
  • 138
32
votes
2 answers

How to append enumerable collection to an existing list in C#

i have three functions which are returning an IEnumerable collection. now i want to combine all these into one List. so, is there any method by which i can append items from IEnumerable to a list. i mean without for each loop?
Radhi
  • 6,289
  • 15
  • 47
  • 68
32
votes
2 answers

Is this a breaking change between AutoMapper 2.0.0 and 2.2.0?

I updated from AutoMapper 2.0.0 to 2.2.0 today and realized the update broke some code. Wanted to ask about it here before posting as an issue on the automapper github site. One of my destination types initializes a collection property like…
danludwig
  • 46,965
  • 25
  • 159
  • 237
31
votes
1 answer

LINQ ToListAsync expression with a DbSet

I have coded a C# MVC5 Internet application, and have a question about using the .ToListAsync LINQ expression. Here is my code that works in an Index action result: IEnumerable mapLocationImageGalleries = await…
Simon
  • 7,991
  • 21
  • 83
  • 163
31
votes
6 answers

Is yield return in C# thread-safe?

I have the following piece of code: private Dictionary items = new Dictionary; public IEnumerable Keys { get { foreach (object key in items.Keys) { yield return key; …
Albic
  • 3,559
  • 4
  • 22
  • 25
30
votes
4 answers

.NET Entity Framework - IEnumerable VS. IQueryable

Please see this line of code. This is an invocation of a stored procedure, which returns an ObjectResult. In order to extract the long values I added the Select: dbContext.FindCoursesWithKeywords(keywords).Select(l => l.Value); Based on…
justabuzz
  • 842
  • 1
  • 9
  • 19
29
votes
3 answers

An extension method on IEnumerable needed for shuffling

I need an extension method which will shuffle an IEnumerable. It can also take an int to specify the size of the returned IEnumerable. Better keeping Immutability of the IEnumerable. My current solution for IList- public static IList
Gulshan
  • 3,611
  • 5
  • 35
  • 46
29
votes
11 answers

IEnumerable as return type

Is there a problem with using IEnumerable as a return type? FxCop complains about returning List (it advises returning Collection instead). Well, I've always been guided by a rule "accept the least you can, but return the maximum." From…
Valentin V
  • 24,971
  • 33
  • 103
  • 152
29
votes
4 answers

Concatenate multiple IEnumerable

I'm trying to implement a method to concatenate multiple Lists e.g. List l1 = new List { "1", "2" }; List l2 = new List { "1", "2" }; List l3 = new List { "1", "2" }; var result = Concatenate(l1, l2,…
fubo
  • 44,811
  • 17
  • 103
  • 137
29
votes
1 answer

How to use Except method in list in c#

I had create a two int type list and assign the items that are only in list1 to list1 using except method. for eg List list1 = new List(); List list2 = new List(); list1 = {1,2,3,4,5,6} // get items from the database list2 =…
Lakhae
  • 1,809
  • 5
  • 25
  • 40
28
votes
4 answers

DataTables vs IEnumerable

I'm having a debate with another programmer I work with. For a database return type, are there any significant memory usage or performance differences, or other cons which should make someone avoid using the DataSets and DataTables and favour types…
CRice
  • 12,279
  • 7
  • 57
  • 84
28
votes
5 answers

Is there ever a reason to not use 'yield return' when returning an IEnumerable?

Simple example - you have a method or a property that returns an IEnumerable and the caller is iterating over that in a foreach() loop. Should you always be using 'yield return' in your IEnumerable method? Is there ever a reason not to? While I…
Jeremy Wiggins
  • 7,239
  • 6
  • 41
  • 56
28
votes
2 answers

Equality between two enumerables

I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true. As a side question, the code below to compare each element works, but there must be a more elegant way var other = (ActivityService) obj; if…
Berryl
  • 12,471
  • 22
  • 98
  • 182