tags:

views:

41

answers:

1

I am using the following class

class Country
{
   int CtryID {get; set;}
   List<City> city {get; set;}
}

class City
{
   string county {get; set;}    
   int sqkm {get; set;}
}

The CtryID has values like 1,2,3 and county has values like "County 1" , "County 2" and so on

I want a result like this

1
   County 1
   County 6
   County 3
2   
   County 9
   County 4
   County 2

How can I achieve this using a Lambda query?

This is what i have used. The list is called ListA

 var lst = from aa in ListA
                    from cny in aa.ctryid
                    select new
                    {
                        CountryID= aa.CtryID,
                        CountyName= cny.County
                    };

Query 2: Also in my second query (seperate from first one) I want to orderby the Country and then each City inside the Country, without repeating the Country multiple times for each City?

A: 
var countryGroups = (from aa in ListA
                    from cny in aa.ctryid
                    select new
                    {
                        CountryID= aa.CtryID,
                        CountyName= cny.County
                    }).GroupBy(n => n.CountryID);

This will group your results by CountryID, but the output won't be a list -- it will be the groupings.

To output, iterate over the countryGroups, and in each of those iterate over the CountyName objects.

Jay