.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 }

 

转载于:https://www.cnblogs.com/Lvkang/p/9354739.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值