C# 从 Collection 到 List<T> 的最快转换

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/187913/
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-03 17:14:11  来源:igfitidea点击:

Fastest Convert from Collection to List<T>

c#collections

提问by t3rse

What I'd like to avoid:

我想避免什么:

ManagementClass m = new ManagementClass("Win32_LogicalDisk");

ManagementObjectCollection managementObjects = m.GetInstances();

List<ManagementObject> managementList = new List<ManagementObject>();

foreach(ManagementObject m in managementObjects){

    managementList.Add(m);

}

Isn't there a way to get that collection into a List that looks something like:

有没有办法将该集合放入一个类似于以下内容的列表中:

List<ManagementObject> managementList = new List<ManagementObjec>(collection_array);

采纳答案by Marc Gravell

What version of the framework? With 3.5 you could presumably use:

什么版本的框架?使用 3.5,您大概可以使用:

List<ManagementObject> managementList = managementObjects.Cast<ManagementObject>().ToList();

(edited to remove simpler version; I checked and ManagementObjectCollectiononly implements the non-generic IEnumerableform)

(编辑以删除更简单的版本;我检查并ManagementObjectCollection仅实现了非通用IEnumerable形式)

回答by steffenj

You could try:

你可以试试:

List<ManagementObject> managementList = new List<ManagementObject>(managementObjects.ToArray());

Not sure if .ToArray() is available for the collection. If you do use the code you posted, make sure you initialize the List with the number of existing elements:

不确定 .ToArray() 是否可用于集合。如果您确实使用了您发布的代码,请确保使用现有元素的数量初始化 List:

List<ManagementObject> managementList = new List<ManagementObject>(managementObjects.Count);  // or .Length

回答by MagicKat

As long as ManagementObjectCollection implements IEnumerable<ManagementObject> you can do:

只要 ManagementObjectCollection 实现 IEnumerable<ManagementObject> 你就可以做到:

List<ManagementObject> managementList = new List<ManagementObjec>(managementObjects);

If it doesn't, then you are stuck doing it the way that you are doing it.

如果没有,那么您就只能按照自己的方式去做。

回答by mancaus

managementObjects.Cast<ManagementBaseObject>().ToList();is a good choice.

managementObjects.Cast<ManagementBaseObject>().ToList();是个不错的选择。

You could improve performance by pre-initialising the list capacity:

您可以通过预先初始化列表容量来提高性能:


    public static class Helpers
    {
        public static List<T> CollectionToList<T>(this System.Collections.ICollection other)
        {
            var output = new List<T>(other.Count);

            output.AddRange(other.Cast<T>());

            return output;
        }
    }

回答by mancaus

you can convert like below code snippet

您可以像下面的代码片段一样进行转换

Collection<A> obj=new Collection<return ListRetunAPI()>

回答by stoto

Since 3.5, anything inherited from System.Collection.IEnumerable has the convenient extension method OfType available.

从 3.5 开始,从 System.Collection.IEnumerable 继承的任何东西都有方便的扩展方法 OfType 可用。

If your collection is from ICollection or IEnumerable, you can just do this:

如果您的集合来自 ICollection 或 IEnumerable,您可以这样做:

List<ManagementObject> managementList = ManagementObjectCollection.OfType<ManagementObject>().ToList();

Can't find any way simpler. : )

找不到任何更简单的方法。:)

回答by jacobsgriffith

You could use

你可以用

using System.Linq;

That will give you a ToList<> extension method for ICollection<>

这将为您提供 ICollection<> 的 ToList<> 扩展方法