Home > DeveloperSection > Forums > Return list of string from list nested in a list
Tanuj Kumar

Total Post:134

Points:940
Posted on    December-12-2013 1:51 AM

 C# C# 
Ratings:


 1 Reply(s)
 788  View(s)
Rate this:

I have three classes; State, County, and City. State contains a list of County. County contains a list of City.

public class State

{

  public string StateCode;

  List<County> Counties;

  List<string> CitiesInState

  {

    get

    {

      return //LINQ STATEMENT TO GET LIST OF ALL CITYNAMES FROM LIST OF COUNTIES

    }

  }

}

 

public class County

{

  public string CountyName;

  List<City> Cities;

}

 

public class City

{

  public string CityName;

}

I am trying to return a list of all CityNames within the State. Since this is a property of an item that is in a List which is an item within another List, I'm not sure it is possible. I haven't been able to write anything that will even compile. Is this possible? Can someone point my in the right direction?



Pravesh Singh

Total Post:411

Points:2881
Posted on    December-12-2013 2:25 AM

Hi Tanuj,

You're asking how to flatten a nested list:

Counties.SelectMany(c => c.Cities).Select(c => c.CityName)


Don't want to miss updates? Please click the below button!

Follow MindStick