读书人

求解答~

发布时间: 2012-06-05 13:54:06 作者: rapoo

求各位高手解答~~~
[code=C#][/code]

string countryDHLId = "Area" + countryInfo.DHLArea.ToString();

var info = from df in db.DHLFreights
where df.WeightMax >= weight && df.WeightMin <= weight
select new
{
countryDHLId
};

double dhlFreight = double.Parse(info);

我想查DHLFreights 的某一个字段 countryDHLId 可能是Area1~~~Area9, Area1这个字段的类型是string 可是为什么 不能把info转化为double类型呢

[解决办法]

C# code
string countryDHLId = "Area" + countryInfo.DHLArea.ToString();var info = from df in db.DHLFreights  where df.WeightMax >= weight && df.WeightMin <= weight  select  GetPropertyValue(df,countryDHLId); double dhlFreight = double.Parse(info.FirstOrDefault());private static object GetPropertyValue(object obj, string property)  {      System.Reflection.PropertyInfo propertyInfo=obj.GetType().GetProperty(property);      return propertyInfo.GetValue(obj, null);  }
[解决办法]
info是一个集合列表

应该取其中一个值进行转化

double dhlFreight = double.Parse(info.First().countryDHLId);

[解决办法]
LINQ 返回的是一个集合。
取出集合的第一个元素用.First()
double.Parse(info.First().countryDHLId)

读书人网 >.NET

热点推荐