C# 将 IEnumerable<T> 转换为 List<T>

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/961375/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 04:18:04  来源:igfitidea点击:

Casting IEnumerable<T> to List<T>

c#listienumerable

提问by RCIX

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

我想知道是否可以将 an 转换IEnumerable为 a List。除了将每个项目复制到列表中之外,还有什么方法可以做到吗?

采纳答案by dmnd

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 IEnumerablemay be generating the elements lazily.

正如已经建议的那样,使用yourEnumerable.ToList(). 它枚举您的IEnumerable,将内容存储在一个新的List. 您不一定要复制现有列表,因为您IEnumerable可能会懒惰地生成元素。

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);
}

回答by John Farrell

//using System.Linq;

//使用 System.Linq;

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

使用 .ToList() 方法。在 System.Linq 命名空间中找到。

var yourList = yourEnumerable.ToList();

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

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

回答by Arsen Mkrtchyan

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>;

回答by Shimmy Weitzhandler

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

创建一个新的 List 并将旧的 IEnumerable 传递给它的初始值设定项:

    IEnumerable<int> enumerable = GetIEnumerable<T>();
    List<int> list = new List<int>(enumerable);

回答by David Ferenczy Rogo?an

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

正如其他人所建议的,只需ToList()在可枚举对象上使用该方法:

var myList = myEnumerable.ToList()

But, if your object implementing the IEnumerableinterfacedoesn't have the ToList()methodand you're getting an error like the following:

但是,如果您实现IEnumerable接口ToList()的对象没有该方法,并且您会收到如下错误:

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

“IEnumerable”不包含“ToList”的定义

...you're probably missing the System.Linqnamespace, because theToList()method is an extension methodprovided by that namespace, it's not a member of the IEnumerableinterface itself.

...您可能缺少System.Linqnamespace,因为该方法是该命名空间提供的扩展方法,它不是接口本身的成员。ToList()IEnumerable

So just add the namespaceto your source file:

因此,只需将命名空间添加到您的源文件中:

using System.Linq