开发者

How to iterate through a list of an anonymous type

开发者 https://www.devze.com 2022-12-08 07:10 出处:网络
I have a list of Countries and i would like to separately get the name of the country and the CountryID.

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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消