Chris Anderson

Chris Anderson

  • NA
  • 93
  • 8.3k

Get the properties from array in object to populate dataGridView

Feb 12 2021 6:02 PM
I'm trying to get the properties from my Toys array which is in the Animal object.
 
My current attempt which works fine til I'm trying to reach the fields of the Toy object from the Toys array:
  1. var anonTypeArr = Petowner.Pets.Select(  
  2.     x => new {   
  3.         x.Age,   
  4.         x.Name,  
  5.         x.Toys.Select(  // Psuedo code. How do I get fields from the Toys array here?  
  6.             y => new    //  
  7.             {           //  
  8.                 y.Name, //  
  9.                 y.Color //  
  10.             }           //  
  11.         )               //  
  12.     }).ToArray();       ///  
  13. dataGridView1.DataSource = anonTypeArr;  
My animal class:
  1. abstract class Animal  
  2. {  
  3.     public string Name { getset; }  
  4.     public virtual int Age { getset; }  
  5.     public List Toys { getset; } = new List();  // How can I get the fields from a toy object?
  6. }
 
My toy class
  1. abstract class Toy  
  2. {  
  3.         public virtual string Name { get; set; }
  4.         public virtual string Color { getset; }   
  5.         public virtual string Size { getset; }  
  6.         public int Quality { getset; } = 10;  
  7.   
  8. }  
 

Answers (1)