I have a List which contains some values.
Example:
List<object> testData = new List <object>();
testData.Add(new List<object> { "aaa", "bbb", "ccc" });
testData.Add(new List<object> { "ddd", "eee", "fff" });
testData.Add(new List<object> { "ggg", "hhh", "iii" });
And I have a class like
class TestClass
{
public string AAA {get;set;}
public string BBB {get;set;}
public string CCC {get;set;}
}
How to convert the testData to the type List<TestClass> ?
Is there a way to convert other than this?
testData.Select(x => new TestClass()
{
AAA = (string)x[0],
BBB = (string)x[1],
CCC = (string)x[2]
}).ToList();
I don’t want to mention the column names, so that I can use this code irrespective of class changes.
I also have a IEnumerable<Dictionary<string, object>> which has the data.
You have to explicitly create the TestClass objects, and moreover cast the outer objects to
List<object>and the inner objects to strings.You could also create a constructor in TestClass that takes
List<object>and does the dirty work for you:Then: