I have a list of a custom car object List<Car>
. The car class has two properties 'BrandName' and 'Models' as defined below.
public class Car
{
public string BrandName {get; set;}
public List<string> Models {get; set;}
}
The data for the 'List` object is populated from an API as below. It returns close to 200 rows. For illustration purposes, the object has been instantiated with two items as below. The object is returned from the API with this structure and I have no control over how the data is structured and sent.
List<Car> listCarObj = new List<Car>(){
new Car(){ BrandName = "Mercedes", Models = new List<string>(){"Class A", "Class E"}},
new Car(){ BrandName = "BMW", Models = new List<string>(){"X Series", "E Series"}}
}
How do I convert this list to an IEnumerable or another List of an anonymous type having data in the below format using Linq?
var obj = new[] {new {brand = "Mercedes", model = "Class A"},
new {brand = "Mercedes", model = "Class E"},
new {brand = "BMW", model = "X Series"},
new {brand = "BMW", model = "E Series"}};
Thanks in advance..