353

I want to convert from IEnumerable<Contact> to List<Contact>. How can I do this?

Koterpillar
  • 7,883
  • 2
  • 25
  • 41
kartal
  • 17,436
  • 34
  • 100
  • 145

5 Answers5

570

You can do this very simply using LINQ.

Make sure this using is at the top of your C# file:

using System.Linq;

Then use the ToList extension method.

Example:

IEnumerable<int> enumerable = Enumerable.Range(1, 300);
List<int> asList = enumerable.ToList();
jpaugh
  • 6,634
  • 4
  • 38
  • 90
vcsjones
  • 138,677
  • 31
  • 291
  • 286
  • 51
    It is important to note that your solution works for the generic version of `IEnumerable`. The answer from user pickles below handles the non-generic version. – mkmurray Mar 20 '13 at 19:49
  • 3
    I wonder if Microsoft changed this. I just tried this very example, and there is no ToList() method in IEnumerable. Using VS 2015 and .NET 4.6.1 . – James Dec 22 '16 at 20:20
  • 3
    @James it's an extension method. You may need the "using", but no, they did not change this. – vcsjones Dec 22 '16 at 20:27
  • 1
    will it throw an error if my IEnumerable is empty ? – Sumit Joshi Feb 15 '18 at 06:04
223

In case you're working with a regular old System.Collections.IEnumerable instead of IEnumerable<T> you can use enumerable.Cast<object>().ToList()

cordialgerm
  • 8,403
  • 5
  • 31
  • 47
26

If you're using an implementation of System.Collections.IEnumerable you can do like following to convert it to a List. The following uses Enumerable.Cast method to convert IEnumerable to a generic List.

// ArrayList implements IEnumerable interface
ArrayList _provinces = new System.Collections.ArrayList();
_provinces.Add("Western");
_provinces.Add("Eastern");

List<string> provinces = _provinces.Cast<string>().ToList();

If you're using Generic version IEnumerable<T>, The conversion is straight forward. Since both are generics, you can do like below,

IEnumerable<int> values = Enumerable.Range(1, 10);
List<int> valueList = values.ToList();

But if the IEnumerable is null, when you try to convert it to a List, you'll get an ArgumentNullException saying Value cannot be null.

IEnumerable<int> values2 = null;
List<int> valueList2 = values2.ToList();

enter image description here

Therefore as mentioned in the other answer, remember to do a null check before converting it to a List.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Nipuna
  • 6,846
  • 9
  • 64
  • 87
7

I use an extension method for this. My extension method first checks to see if the enumeration is null and if so creates an empty list. This allows you to do a foreach on it without explicitly having to check for null.

Here is a very contrived example:

IEnumerable<string> stringEnumerable = null;
StringBuilder csv = new StringBuilder();
stringEnumerable.ToNonNullList().ForEach(str=> csv.Append(str).Append(","));

Here is the extension method:

public static List<T> ToNonNullList<T>(this IEnumerable<T> obj)
{
    return obj == null ? new List<T>() : obj.ToList();
}
Sнаđошƒаӽ
  • 16,753
  • 12
  • 73
  • 90
Craig B
  • 333
  • 5
  • 8
6

another way

List<int> list=new List<int>();

IEnumerable<int> enumerable =Enumerable.Range(1, 300);  

foreach (var item in enumerable )  
{     
  list.Add(item);  
}
Grant Shannon
  • 4,709
  • 1
  • 46
  • 36
  • 3
    It's quite better to use "AddRange(enumerable)" function or List(enumerable) constructor. – QtRoS Sep 22 '15 at 14:41