0

I googled and fined this link, but still not working Convert List<T> to object[]

I want to convert the list of int to object array. Why? because I want to add the list as object array in Combbox, it's the argument.

The problem is it adds just "one item" Object[] Array in Combobox, while "tempList" contains 4 items of type int.

I like to add the 4 items in the object[] (object array), now it adds as 1 item and just shows Object[] array in the debugger.

When I look in debugger and type :

customers - it shows object[1] and when I type

customers[0] it shows object[4], so in fact 4 items are added, but how can I get these values.???

List<int> tempList= new CustomersRepository().GetAll().Select(a => a.Numero).ToList();
object[] customers = new object[] { tempList.Cast<object>().ToArray() };
ComboBox.Items.AddRange(customers);
Community
  • 1
  • 1
ethem
  • 2,888
  • 9
  • 42
  • 57

5 Answers5

4

What you are doing is currently creating an Array of arrays. So accessing the values would be done by the following :

customers[0][1]

What I suspect you are actually looking for is the following :

object[] customers = tempList.Cast<object>().ToArray();

This will create an Array of object items called customers.

Nunners
  • 3,047
  • 13
  • 17
3
List<int> tempList= ...;
object[] customers  = tempList.Cast<Object>().ToArray();
Sriram Sakthivel
  • 72,067
  • 7
  • 111
  • 189
2

Try it this way :

var customers = tempList.Cast<object>().ToArray();

Or also with an explicit cast :

var customers = tempList.Select(t => (object)t).ToArray();

The problem occured because you are using an initializer for building your list.

This syntax :

var arr = new object[] { "a", "b" }

initialize an array with two strings.

So when you are writing

var arr = new object[] { tempList.Cast<object>().ToArray() }

you build an array of one array!

Cyril Gandon
  • 16,830
  • 14
  • 78
  • 122
2
object[] customers = new object[] { tempList.Cast<object>().ToArray() };

Here you create an object[] with one item: another object[] containing the items of tempList.


Just use object[] customers = tempList.Cast<Object>().ToArray() instead of wrapping it in another object[].

sloth
  • 99,095
  • 21
  • 171
  • 219
0

You dont need ToList() if you anyway want to process further and obtain an array later. Something like this should be more efficient:

var temp = new CustomersRepository().GetAll().Select(a => a.Numero);
object[] customers = temp.Cast<object>().ToArray();
ComboBox.Items.AddRange(customers);

Had temp been a collection of reference types, you need not cast at all, but rely on array covariance. This should work:

var temp = new CustomersRepository().GetAll().Select(a => a.StringProperty);
object[] customers = temp.ToArray(); //no need of Cast<object>
ComboBox.Items.AddRange(customers);

But this doesn't work in your case since array covariance doesnt support for value types.


Another idea is to have an extension method AddRange that accepts any IEnumerable, not just object[]. Something like:

public static void AddRange(this IList list, IEnumerable lstObject)
{
    foreach (T t in lstObject)
        list.Add(t);
}

Now you can call:

var customers = new CustomersRepository().GetAll().Select(a => a.Numero);
ComboBox.Items.AddRange(customers);

The best of all is to add the Customers as such, and set the DisplayMember or ValueMember properties to your property, like Numero. Since you have the entire objects in combobox you have more info with you. Something like:

ComboBox.DisplayMember = "Numero";
var customers = new CustomersRepository().GetAll();
ComboBox.Items.AddRange(customers); // using the above extension method
Community
  • 1
  • 1
nawfal
  • 70,104
  • 56
  • 326
  • 368