56

I was wondering if it is possible to cast an IEnumerable to a List. Is there any way to do it other than copying out each item into a list?

David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
RCIX
  • 38,647
  • 50
  • 150
  • 207

6 Answers6

65

As already suggested, use yourEnumerable.ToList(). It enumerates through your IEnumerable, storing the contents in a new List. You aren't necessarily copying an existing list, as your IEnumerable may be generating the elements lazily.

This is exactly what the other answers are suggesting, but clearer. Here's the disassembly so you can be sure:

public static List<TSource> ToList<TSource>(this IEnumerable<TSource> source)
{
    if (source == null)
    {
        throw Error.ArgumentNull("source");
    }
    return new List<TSource>(source);
}
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
dmnd
  • 2,440
  • 2
  • 22
  • 28
52
using System.Linq;

Use the .ToList() method. Found in the System.Linq namespace.

var yourList = yourEnumerable.ToList();

https://learn.microsoft.com/en-us/dotnet/api/system.linq?view=netcore-2.2

user366312
  • 16,949
  • 65
  • 235
  • 452
John Farrell
  • 24,673
  • 10
  • 77
  • 110
  • 5
    If not available using System.Linq is missing :). This should totally be the accepted answer... – CodingYourLife Feb 02 '17 at 16:55
  • 1
    Also if it's not available, you may have forgotten to declare your IEnumerable with its . – Keavon Feb 11 '19 at 22:52
  • Very simple and to the point, thanks! Maybe use a null-conditional operator in `yourEnumerable?.ToList();` to take into account the possibility that `yourEnumerable` might be `null`? – Jean-David Lanz Nov 17 '20 at 09:14
7

As others suggested, simply use the ToList() method on an enumerable object:

var myList = myEnumerable.ToList()

But, if your object implementing the IEnumerable interface doesn't have the ToList() method and you're getting an error like the following:

'IEnumerable' does not contain a definition for 'ToList'

...you're probably missing the System.Linq namespace, because the ToList() method is an extension method provided by that namespace, it's not a member of the IEnumerable interface itself.

So just add the namespace to your source file:

using System.Linq
David Ferenczy Rogožan
  • 23,966
  • 9
  • 79
  • 68
3

Create a new List and pass the old IEnumerable to its initializer:

    IEnumerable<int> enumerable = GetIEnumerable<T>();
    List<int> list = new List<int>(enumerable);
Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632
2

Another gotcha (async call)

An async call may be your problem. If you added the using System.Linq statement and you are still getting the error "does not contain a definition for 'ToList' and no accessible extension method...", look carefully for the Task keyword in your error message.

Original Call (works)

IEnumerable<MyDocument> docList = await _documentRepository.GetListAsync();

Attempt to use ToList (still not working)

So...if you are doing this and it does NOT work

List<MyDocument> docList = await _documentRepository.GetListAsync().ToList();

Use parenthesis

You are actually calling ToList on the Task<IEnumerable>! Add parenthesis around your await call like this

List<MyDocument> docList = (await _documentRepository.GetListAsync()).ToList();
David Yates
  • 1,935
  • 2
  • 22
  • 38
0

no, you should copy, if you are sure that the reference is reference to list, you can convert like this

List<int> intsList = enumIntList as List<int>;
Arsen Mkrtchyan
  • 49,896
  • 32
  • 148
  • 184
  • 5
    If you're *sure* that it's a reference to a list, you should use a direct cast so that it will throw an exception if you're wrong. Use "as" if you think it *might* be a List but you're not sure, and neither are error conditions. Then test whether the result is null. – Jon Skeet Jun 07 '09 at 07:31
  • Maybe add an 'if (intsList == null) intsList = new List(enumIntList);' if it *might* be a 'List', already, but there are some cases where it is not. – jerryjvl Jun 07 '09 at 08:22