【发布时间】:2020-03-19 22:35:55
【问题描述】:
我有以下类结构:
public class Parent
{
public int Property1;
public int Property2;
public int Property3;
//Lots more properties
}
public class Child : Parent
{
public int Property80;
}
还有这样的父母名单:
var parentList = new List<Parent>
{
//Some data....
};
我现在想创建一个新的子列表,其中包含来自parentList 的所有数据
我知道我可以做到:
var childList = parentList.Select(x => new Child { Property1 = x.Property1, Property2 = x.Property2, //etc }
但我的问题是,是否有一种速记或单行方式来执行此操作,以免我不得不写出一大串属性?
【问题讨论】:
-
看看stackoverflow.com/questions/16534253/…,也可以考虑使用对象到对象的映射库,比如 AutoMapper。
标签: c# list inheritance casting copy