开发者

Use linq to make a projection of an anonymous class with a list

开发者 https://www.devze.com 2023-04-10 09:27 出处:网络
Suppose I have these classes: public MyClass { public Property1 {get; set;} public Property2 {get; set;} public Property3 {get; set;}

Suppose I have these classes:

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;}
}

Supposing that everything is instantiated, how would I use linq to make a projection that just had Property1 and Property2 and a list that just had SecondProperty4 a开发者_如何学编程nd SecondProperty5?

Something like:

myClass.Select(x=> new { Property1 = x.Property1, 
                         Property2 = x.Property2, 
                         //Some Way To add The list here
                       }

NOTE: Just to clarify myClass is a List<MyClass> object.


myClass.Select(x=> new { Property1 = x.Property1, 
                         Property2 = x.Property2, 
                         SecondList = x.MyList.Select(i => new { i.SecondProperty4, i.SecondProperty5 }).ToList()
                       }
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号