Convert Object Properties to Array

mike75

New member
Joined
Jan 30, 2021
Messages
3
Programming Experience
1-3
C#:
public class myObj
{
    public string ob1 { get; set; }
    public string ob2 { get; set; }
}

List<myObj> objlist = new List<myObj>();

objlist.Add(new myObj
{
    ob1 = "A",
    ob2 = "1"
});
objlist.Add(new myObj
{
    ob1 = "B",
    ob2 = "2"
});

List<string[]> converted = MyConvert(objlist);


public static List<string[]> MyConvert(List<objlist> mobj)
{
    foreach (objlist item in mobj)
    {
        string[] arr = ((IEnumerable)item).Cast<objlist>()
                         .Select(x => x.ToString())
                         .ToArray();
    }
}

I've been trying to convert the object objlist to a List of string array. I've searched the net and found IEnumerable might help, but I got stopped by an error when I run the program...

System.InvalidCastException
HResult=0x80004002
Message=Unable to cast object of type 'objlist' to type 'System.Collections.IEnumerable'.
 
Why would you want to store perfectly good data in objects into a list of strings?

Anyway, you are partway there. You want something like:
C#:
return objList.Select(o => new string[] { o.ob1, o.ob2 })
              .ToList();

The Select() returns an array of strings for each of the objects in the objList. The ToList() converts the IEnumerable<string[]> into a List<string[]>.
 
Why would you want to store perfectly good data in objects into a list of strings?

Anyway, you are partway there. You want something like:
C#:
return objList.Select(o => new string[] { o.ob1, o.ob2 })
              .ToList();

The Select() returns an array of strings for each of the objects in the objList. The ToList() converts the IEnumerable<string[]> into a List<string[]>.

Thanks @Skydiver! Is there a way to automatically enumerate the property names?
There might be over 50 properties in the object and naming it one by one takes a lot of time and lines. ?
 
Why would you want to store perfectly good data in objects into a list of strings?

Anyway, you are partway there. You want something like:
C#:
return objList.Select(o => new string[] { o.ob1, o.ob2 })
              .ToList();

The Select() returns an array of strings for each of the objects in the objList. The ToList() converts the IEnumerable<string[]> into a List<string[]>.

How about returning a 2 dimensional array string[ , ] ? Where the first dimension is the lines and the second the columns.
 
Thanks @Skydiver! Is there a way to automatically enumerate the property names?
There might be over 50 properties in the object and naming it one by one takes a lot of time and lines. ?
You would normally use Reflection for that. Reflection is slow and should generally be avoided in situations that it's not required. If you want to be able to access property values without referring to the property though, that's how you'd do it:
C#:
var items = new List<Thing>();

// Populate list here.

List<string[]> propertiesOfItems = items.Select(st => st.GetType()
                                                        .GetProperties(BindingFlags.Instance | BindingFlags.Public)
                                                        .Select(pi => pi.GetValue(st)?.ToString())
                                                        .ToArray())
                                        .ToList();
 
How about returning a 2 dimensional array string[ , ] ? Where the first dimension is the lines and the second the columns.
And why would you want to do that? Why are you hiding the reason for this request? If it's just for the sake of learning then that's fine, but just say that. If there's a genuine reason for it then tell us, because I suspect that that reason is misguided and you would be better off doing something else. We would like to help by providing the best solution but we can't do that if we don't even know what problem you're actually trying to solve. 2D arrays have their place but good uses for them are very rare and this certainly doesn't look like such a use, so it's pretty much guaranteed that you're asking us how to do the wrong thing.
 
With regards to post #6, the code can look daunting to the uninitiated but it's not especially complex. The outer Select gets a string array of property values for each list item and then creates a generic List of those arrays. The inner Select gets a string array of property values for one item using Reflection. It gets a Type object for the item's type, gets an array of PropertyInfo objects for the public, instance properties of that type, gets the value of each of those properties and converts them to string objects and then puts the results in an array. I haven't tested that code but I believe that that is the correct combination of BindingFlags values, assuming that you really do want just public, instance properties.
 
Back
Top Bottom