Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert List<double> into object[]

Tags:

c#

I have some dynamic list collection and this is added on run time. I want to convert those ListColl in to array of object. The below is my code

 List<double> ListColl = new List<double>();
 // This is added dynamically and finally want to convert this list into object array
  values.Add(10);
  values.Add(40);
  values.Add(20);

Expected output is like this

object[] objArray = new object[] { 10, 40, 20 };

I used object[] objArray = ListColl.ToArray(). But this is throwing error.

How to convert this list collection in to array of object

like image 907
Bharathi Avatar asked Dec 05 '25 19:12

Bharathi


2 Answers

You can use Cast<object>() call followed by ToArray():

object[] objArray = ListColl.Cast<object>().ToArray();
like image 144
Sergey Kalinichenko Avatar answered Dec 08 '25 10:12

Sergey Kalinichenko


Method Syntax:

object[] objArray = ListColl.Cast<object>().ToArray();

Query Syntax:

var objArray = (from item in ListColl select item as object).ToArray();
like image 20
Masoud Andalibi Avatar answered Dec 08 '25 08:12

Masoud Andalibi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!