Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to iterate through a list of an anonymous type

I have a list of Countries and i would like to separately get the name of the country and the CountryID.

To be more understandable please visit this link where i have uploaded an image

alt text http://img251.imageshack.us/img251/8505/listx.jpg

http://img251.imageshack.us/img251/8505/listx.jpg

How is this possible with an anonymous type?

I tried Countries[0]["Country"] but with no luck.

like image 907
profanis Avatar asked Oct 14 '09 16:10

profanis


1 Answers

Just loop over your list - the anonymous type is anonymous - but still a strict type! You can get intellisense and should be able to access the fields Country and CountryID with no problem at all:

foreach(var c in yourListOfCountries)
{
   string countryName = c.Country;
   int countryID = c.CountryID;
}

Marc

like image 63
marc_s Avatar answered Sep 18 '22 09:09

marc_s