Oti*_*iel 3 c# linq coding-style
我有一个List的MyType1对象.我想在一个新List的MyType2对象中复制所有这些对象,有一个构造函数MyType2,它将一个MyType1对象作为参数.
现在,我这样做:
List<MyType1> type1objects = new List<MyType1>();
// Fill type1objects
// ...
List<MyType2> type2objects = new List<MyType2>();
foreach (MyType1 type1obj in type1objects) {
MyType2 type2obj = new MyType2(type1obj);
type2objects.Add(type2obj);
}
Run Code Online (Sandbox Code Playgroud)
有没有办法做到这一行(我想也许Linq可能)?
var type2objects = type1objects.Select(o => new MyType2(o)).ToList();
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
131 次 |
| 最近记录: |