.NET获取城市信息(将三字代码转换成城市名)
整理代码,发现有一个从两张表里读取城市列表,然后linq和lambda表达式来获取城市名的函数,代码如下:
1 public static string GetCityHotelText(string city, int VerNum) 2 { 3 string ucbcField, airPortField; 4 string cityText = ucbcField = airPortField = ""; 5 //vernum控制中英文显示版本 6 if (VerNum == 1) 7 { 8 ucbcField = "CityNameEN"; 9 airPortField = "CityName_en"; 10 } 11 else 12 { 13 ucbcField = "CityNameCN"; 14 airPortField = "CityName_zh"; 15 } 16 //两张表,datatable,读表的方法就不写了 17 DataTable ucbcs = null; 18 DataTable airPorts =null; 19 try 20 { 21 var query = ucbcs.AsEnumerable().Where(ucbc => ucbc.Field<string>("CityCode").ToLower() == city.ToLower()); 22 if (query.Any()) 23 { 24 cityText = query.First().Field<string>(ucbcField); 25 } 26 else 27 { 28 var query2 = from airPort in airPorts.AsEnumerable() 29 where airPort.Field<string>("Code").ToLower() == city.ToLower() 30 select airPort; 31 if (query2.Any()) 32 { 33 cityText = query2.First().Field<string>(airPortField); 34 } 35 } 36 } 37 catch (Exception e) 38 { 39 } 40 if (string.IsNullOrEmpty(cityText)) 41 { 42 cityText = city; 43 } 44 return cityText; 45 }