开发者

Retrieve field values based on field name

I inherited a database in which the individual data points are stored horizontally in fields rather than vertically. In other words, in addition to other fields, each record (which represents a particular channel from which data was obtained) there are 250 fields whose names range from "P0001" to "P0250".

I 开发者_如何学JAVAwant to retrieve the data from each PXXXX field and store it in a List(of Double). Assuming R250 is the record that has these fields, how do I construct the LINQ query to obtain this data?


    For Each pn In Type.GetType(R250.ToString).GetProperties.Where(Function(n) n.Name.StartsWith("P")).Select(Function(m) m.Name)
        Console.WriteLine(Type.GetType(R250.ToString).GetProperty(pn).GetValue(R250, Nothing))
    Next


You could use reflection such as:

List<double> theList = new List<double>();
for (int i = 1; i <=250; i++)
{
   PropertyInfo fiField = R250.GetType().GetProperty("P" + i.ToString().PadLeft(4));
   if (fiField != null && fiField.PropertyType == typeof(double))
      theList.Add((double)fiField.GetValue(R250, new object[] { }));
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜