C # - convert an array of types to a general list

Code snippet:

ShippingPeriod[] arrShippingPeriods;
.
.
.

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

The last line will not compile, and I get a message:

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

+3
source share
4 answers

try the following:

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 the array.

+11
source

As others have said, you need to call ToListin your array. You have not shown what it is ShippingPeriodList.

, , , , . , :

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

:

ShippingPeriod [] arrShippingPeriods;

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

IEnumerable, List.

Hope this helps

+3
source

I got a solution !!!

You need to put "using System.Linq;" in the title of your class, and that’s it. When you add this, the array recognizes the toList () command.

0
source

Source: https://habr.com/ru/post/1712075/


All Articles