C# - 将类型数组转换为通用列表

时间:2021-01-04 16:05:59

Code Snippet:

ShippingPeriod[] arrShippingPeriods;
.
.
.

List<ShippingPeriod> shippingPeriods = ShippingPeriodList.ToList<ShippingPeriod>();

The Last line won't compile and the error I get is:

最后一行不会编译,我得到的错误是:

"'ShippingPeriod[]' does not contain a definition for 'ToList' and the best extension method overload 'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)' has some invalid arguments"

“'ShippingPeriod []'不包含'ToList'的定义,并且最好的扩展方法重载'System.Linq.Enumerable.ToList(System.Collections.Generic.IEnumerable)'有一些无效的参数”

4 个解决方案

#1


try this:

ShippingPeriod [] arrShippingPeriods;

//init and populate array

IList<ShippingPeriods> lstShippingPeriods = 
                  arrShippingPeriods.ToList<ShippingPeriods>();

You need to call ToList on the array object not the class of objects contained in array.

您需要在数组对象上调用ToList而不是数组中包含的对象类。

#2


As others have said, you need to call ToList on your array. You haven't shown what ShippingPeriodList is.

正如其他人所说,你需要在你的阵列上调用ToList。您尚未显示ShippingPeriodList是什么。

However, when you get that bit right, note that you won't need to provide the type argument, as type inference will do it for you. In other words, this should work fine:

但是,当你理解这一点时,请注意你不需要提供类型参数,因为类型推断会为你做。换句话说,这应该工作正常:

List<ShippingPeriod> list = arrShippingPeriods.ToList();

#3


Another option would be:

另一种选择是:

ShippingPeriod [] arrShippingPeriods;

var lstShippingPerios=new List<ShippingPeriod>(arrShippingPeriods);

Since arrays already implement IEnumerable, you can pass it to the List constructor.

由于数组已经实现了IEnumerable,因此可以将它传递给List构造函数。

Hope this helps

希望这可以帮助

#4


I got the solution!!!

我得到了解决方案!

You have to put "using System.Linq;" in the header of your Class, and that´s it. When you put that, the array recognizes toList() command.

你必须把“使用System.Linq;”在你的班级的标题中,就是这样。放置它时,数组识别toList()命令。

#1


try this:

ShippingPeriod [] arrShippingPeriods;

//init and populate array

IList<ShippingPeriods> lstShippingPeriods = 
                  arrShippingPeriods.ToList<ShippingPeriods>();

You need to call ToList on the array object not the class of objects contained in array.

您需要在数组对象上调用ToList而不是数组中包含的对象类。

#2


As others have said, you need to call ToList on your array. You haven't shown what ShippingPeriodList is.

正如其他人所说,你需要在你的阵列上调用ToList。您尚未显示ShippingPeriodList是什么。

However, when you get that bit right, note that you won't need to provide the type argument, as type inference will do it for you. In other words, this should work fine:

但是,当你理解这一点时,请注意你不需要提供类型参数,因为类型推断会为你做。换句话说,这应该工作正常:

List<ShippingPeriod> list = arrShippingPeriods.ToList();

#3


Another option would be:

另一种选择是:

ShippingPeriod [] arrShippingPeriods;

var lstShippingPerios=new List<ShippingPeriod>(arrShippingPeriods);

Since arrays already implement IEnumerable, you can pass it to the List constructor.

由于数组已经实现了IEnumerable,因此可以将它传递给List构造函数。

Hope this helps

希望这可以帮助

#4


I got the solution!!!

我得到了解决方案!

You have to put "using System.Linq;" in the header of your Class, and that´s it. When you put that, the array recognizes toList() command.

你必须把“使用System.Linq;”在你的班级的标题中,就是这样。放置它时,数组识别toList()命令。