如何遍历匿名类型的列表

pro*_*nis 4 .net c# asp.net anonymous-types

我有一个国家列表,我想分别获取国家名称和CountryID.

为了更容易理解,请访问我上传图像的链接

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

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

匿名类型怎么可能?

我试过了国家[0] ["国家"],但没有运气.

mar*_*c_s 20

只需循环遍历您的列表 - 匿名类型是匿名的 - 但仍然是严格的类型!您可以获得intellisense,并且应该能够访问这些字段Country并且CountryID完全没有问题:

foreach(var c in yourListOfCountries)
{
   string countryName = c.Country;
   int countryID = c.CountryID;
}
Run Code Online (Sandbox Code Playgroud)