【发布时间】:2010-11-13 19:32:32
【问题描述】:
课堂converting a class to another list的问答很酷。如何将 MyData 列表转换为 MyData2 的另一个列表?例如:
List<MyData> list1 = new List<MyData>();
// somewhere list1 is populated
List<MyData2> list2;
// Now I need list2 from list1. How to use similar LINQ or Lambda to get
list2 = ... ?
在这里我试过了,但我无法弄清楚完整的代码:
list2 = (from x in list1 where list1.PropertyList == null
select new MyData2( null, x.PropertyB, x.PropertyC).
Union (
from y in list1 where list.PropertyList != null
select new MyData2( /* ? how to loop each item in ProperyList */
y.PropertyB, y.PropertyC)
).ToList();
其中 MyData2 有一个类似 (string, string, string) 的 CTOR。
【问题讨论】: