leo*_*ora 3 c# linq collections
我有:
IEnumerable<Foo> foolist
Run Code Online (Sandbox Code Playgroud)
我想将其转换为:
IEnumerable<Bar> barlist
Run Code Online (Sandbox Code Playgroud)
是否有linq/lambda解决方案从一个移动到另一个
两个对象(foo和bar)都具有我要转换的简单属性.例如:
bar.MyYear = foo.Year
Run Code Online (Sandbox Code Playgroud)
他们每个人都有大约6个属性
Ree*_*sey 10
你可以做:
IEnumerable<Bar> barlist = foolist.Select(
foo => new Bar(foo.Year)); // Add other construction requirements here...
Run Code Online (Sandbox Code Playgroud)
Enumerable.Select实际上是一个投影函数,因此它非常适合类型转换.从帮助:
将序列的每个元素投影到新表单中.
编辑:
由于Bar没有构造函数(来自您的注释),您可以使用对象初始值设定项:
IEnumerable<Bar> barlist = foolist.Select(
foo => new Bar()
{
Year = foo.Year,
Month = foo.Month
// Add all other properties needed here...
});
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2173 次 |
| 最近记录: |