我有一个列表,其中包含一些值。
I have a List which contains some values.
示例:
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" });
和我有一类像
class TestClass
{
public string AAA {get;set;}
public string BBB {get;set;}
public string CCC {get;set;}
}
如何转换的 TESTDATA
的类型名单,其中,识别TestClass&GT;
有没有一种方法来转换其他比这个?
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();
我不想提列名,这样我就可以不考虑使用类改变这个code。
I don't want to mention the column names, so that I can use this code irrespective of class changes.
我也有一个的IEnumerable&LT;字典&LT;字符串,对象&gt;&GT;
有数据
您必须显式地创建TestClass的对象,而且投外物体名单,其中,对象&gt;
和内对象为字符串。
You have to explicitly create the TestClass objects, and moreover cast the outer objects to List<object>
and the inner objects to strings.
testData.Cast<List<object>>().Select(x => new TestClass() {AAA = (string)x[0], BBB = (string)x[1], CCC = (string)x[2]}).ToList()
您也可以创建TestClass的一个构造函数名单,其中,对象&gt;
和做肮脏的工作适合你:
You could also create a constructor in TestClass that takes List<object>
and does the dirty work for you:
public TestClass(List<object> l)
{
this.AAA = (string)l[0];
//...
}
然后:
testData.Cast<List<object>>().Select(x => new TestClass(x)).ToList()
上一篇:使用的TransactionScope间歇System.ArgumentNullExceptionTransactionScope、System、ArgumentNullException
下一篇:LibGDX:Android的SpriteBatch没有图纸图纸、LibGDX、Android、SpriteBatch