首先已经将国内和国际的城市列表分别存到了表dtcity和dtIcity,
第一种是使用datatable.select找出数据出来存入datarow[]中,然后读取
1 DataRow[] rowscity = dtcity.Select("AirportCode='" +takeoffstn + "'"); 2 if (rowscity.Length < 1) 3 rowscity = dtIcity.Select("AirportCode='" + takeoffstn + "'"); 4 if (rowscity.Length < 1) 5 takeoffstn = "未知城市"; 6 else 7 { 8 takeoffstn = rowscity[0]["AirportCN"].ToString(); 9 }
第二种方法是使用linq,将datatable转换成list<T>形式,然后使用where和lambda表达式来读取
1 List<CityList> cityhost = ExtendMethod.ToList<CityList>(dtcity); 2 List<CityList> cityInt = ExtendMethod.ToList<CityList>(dtIcity); 3 var chost = cityhost.Where(c => c.AirportCode == takeoffstn).ToList(); 4 if (chost == null || chost.Count < 1) 5 chost = cityInt.Where(c => c.AirportCode == takeoffstn).ToList(); 6 if (chost == null || chost.Count < 1) 7 takeoffstn = "未知城市"; 8 else 9 { 10 takeoffstn = chost[0].AirportCN; 11 }
两种方式都可以,不过使用linq的会快一点