简体   繁体   English

铸造 IEnumerable<T> 列出<T>

[英]Casting IEnumerable<T> to List<T>

I was wondering if it is possible to cast an IEnumerable to a List .我想知道是否可以将IEnumerable转换为List Is there any way to do it other than copying out each item into a list?除了将每个项目复制到列表中之外,还有什么方法可以做到吗?

As already suggested, use yourEnumerable.ToList() .正如已经建议的那样,使用yourEnumerable.ToList() It enumerates through your IEnumerable , storing the contents in a new List .它通过您的IEnumerable枚举,将内容存储在一个新的List You aren't necessarily copying an existing list, as your IEnumerable may be generating the elements lazily.您不一定要复制现有列表,因为您的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);
}
using System.Linq;

Use the .ToList() method.使用 .ToList() 方法。 Found in the System.Linq namespace.在 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

As others suggested, simply use the ToList() method on an enumerable object:正如其他人所建议的,只需在可枚举对象上使用ToList()方法:

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接口的对象没有ToList()方法并且您收到如下错误:

'IEnumerable' does not contain a definition for 'ToList' “IEnumerable”不包含“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. ...您可能缺少System.Linq命名空间,因为ToList()方法是该命名空间提供的扩展方法,它不是IEnumerable接口本身的成员。

So just add the namespace to your source file:因此,只需将命名空间添加到您的源文件中:

using System.Linq

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

不,你应该复制,如果你确定引用是对列表的引用,你可以像这样转换

List<int> intsList = enumIntList as List<int>;

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.如果您添加了 using System.Linq 语句,但仍然收到错误“不包含 'ToList' 的定义且没有可访问的扩展方法...”,请仔细查看错误消息中的 Task 关键字。

Original Call (works)原始电话(作品)

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

Attempt to use ToList (still not working)尝试使用 ToList(仍然无法正常工作)

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>!您实际上是在 Task<IEnumerable> 上调用 ToList! Add parenthesis around your await call like this像这样在您的等待调用周围添加括号

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM