Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 将主详细信息对象转换为单个对象_C#_Reflection - Fatal编程技术网

C# 将主详细信息对象转换为单个对象

C# 将主详细信息对象转换为单个对象,c#,reflection,C#,Reflection,假设我们有一个主细节对象,如下所示 public class A { public string PropA{ get; set; } public List<B> obj { get; set; } } public class B { public string PropB{ get; set; } public List<C> obj { get; set; } } public class C { public string PropC{

假设我们有一个主细节对象,如下所示

public class A
{
  public string PropA{ get; set; }
  public List<B> obj { get; set; }
}

public class B
{
  public string PropB{ get; set; }
  public List<C> obj { get; set; }
}

public class C
{
  public string PropC{ get; set; }
}
现在我们需要将数据从主详细信息对象A复制到单个对象D。是否可能?。我怎样才能解决这个问题。您能给我一个通用的方法,将所有类型的主详细信息对象转换为单个对象。谢谢。

您可以使用LINQ的SelectMany:

或者,在查询语法wow中,这看起来更简洁:

A myA = //...
var Ds = from a in myA
         from b in a.obj
         from c in b.obj
         select new D
         {
             PropA = a.PropA, 
             PropB = b.PropB, 
             PropC = c.PropC
         };

谢谢你的回复。我不知道实际的物体深度路径是什么。在这里,C类可能有另一个细节F,F有另一个细节K。这样,它可以使。请告诉我一个常用的方法。谢谢
A myA = //...
var Ds = myA.SelectMany(a => a.obj.SelectMany(b => b.obj.Select(c => new D {
    PropA = a.PropA, 
    PropB = b.PropB, 
    PropC = c.PropC})));
A myA = //...
var Ds = from a in myA
         from b in a.obj
         from c in b.obj
         select new D
         {
             PropA = a.PropA, 
             PropB = b.PropB, 
             PropC = c.PropC
         };