首页 > 解决方案 > 属性的动态映射

问题描述

如果有任何方法可以将子对象属性动态映射或添加到父对象,我很感兴趣?例如,我有以下课程:

public class Parent {
 public int Id { get; set; }
 public string Name { get; set; }
 public ICollection<Child> Children { get; set; }
}

public class Child {
 public int Id { get; set; }
 public string Name { get; set; }
}

我需要以以下格式从后端发送到前端:

public class Dto {
 public int ParentId { get; set; }
 public string ParentName { get; set; }
 public string Child1Name { get; set; }
 public string Child2Name { get; set; }
 public string Child3Name { get; set; }
}

请记住,父对象可以有不同数量的子对象。我需要这种格式的信息,因为它将显示在网格中(见下图)。这样的事情可以实现吗?如何实现?

在此处输入图像描述

标签: c#entity-framework-coreef-core-2.0

解决方案


推荐阅读