c# - Creating lists from another list's item in Linq -


this question has answer here:

i have list of custom car object list<car>. car class has 2 properties 'brandname' , 'models' defined below.

public class car {      public string brandname {get; set;}      public list<string> models {get; set;} } 

the data 'list` object populated api below. returns close 200 rows. illustration purposes, object has been instantiated 2 items below. object returned api structure , have no control on how data structured , 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 convert list ienumerable or list of anonymous type having data in 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..

here approach selectmany , nested select

var result = listcarobj.selectmany(x => x.models.select(y => new { model = y,brand = x.brandname })); 

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -