使用linq使用列表对匿名类进行投影

Vac*_*ano 2 c# linq projection

假设我有这些类:

public MyClass
{
   public Property1 {get; set;}
   public Property2 {get; set;}
   public Property3 {get; set;}
   public Property4 {get; set;}
   public Property5 {get; set;}
   public Property6 {get; set;}
   public Property7 {get; set;}
   public Property8 {get; set;}
   public List<MySecondClass> MyList {get; set;}
}

public MySecondClass
{
   public SecondProperty1 {get; set;}
   public SecondProperty2 {get; set;}
   public SecondProperty3 {get; set;}
   public SecondProperty4 {get; set;}
   public SecondProperty5 {get; set;}
}
Run Code Online (Sandbox Code Playgroud)

假设一切都被实例化,我将如何使用LINQ做,只是有一个投影Property1,并Property2和刚做了一个列表SecondProperty4SecondProperty5

就像是:

myClass.Select(x=> new { Property1 = x.Property1, 
                         Property2 = x.Property2, 
                         //Some Way To add The list here
                       }
Run Code Online (Sandbox Code Playgroud)

注意:只是为了澄清myClass是一个List<MyClass>对象.

Joe*_*Joe 9

myClass.Select(x=> new { Property1 = x.Property1, 
                         Property2 = x.Property2, 
                         SecondList = x.MyList.Select(i => new { i.SecondProperty4, i.SecondProperty5 }).ToList()
                       }
Run Code Online (Sandbox Code Playgroud)