- public class myObj
- {
- public string ob1 { get; set; }
- public string ob2 { get; set; }
- }
- List
objlist = new List (); - 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
mobj) - {
- foreach (objlist item in mobj)
- {
- string[] arr = ((IEnumerable)item).Cast
() - .Select(x => x.ToString())
- .ToArray();
- }
- }
Converting Object Properties to Array
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'.

Sachin SinghRank #66 · 26.2K reputationPosted Jan 31, 2021, 12:17 AM EST
Rijwan AnsariRank #82 · 24.2K reputationPosted Jan 31, 2021, 2:01 AM EST
Ravi PatelRank #439 · 5.7K reputationPosted Jan 31, 2021, 2:21 AM EST
Asma KhalidRank #160 · 14.7K reputationPosted Feb 3, 2021, 10:29 AM EST
Aman GuptaRank #193 · 12.4K reputationPosted Feb 1, 2021, 2:38 AM EST