从通用列表中动态选择列

时间:2012-08-20 23:52:19

标签: vb.net linq

我有一个200列宽的表,需要返回特定行和列的数据,但直到运行时才会知道该列。我可以轻松地将我想要的行放入列表,单独的强类型对象或通过LINQ的数组,但我不能在我的生活中找出如何找到我需要的列。

所以例如(在较小的范围内)我的表看起来像这样

GrowerKey | day1 | day2 | day3  | day4 |
-----------------------------------------
  3       |  1   |   3  |   2   |   2 |
  4       |  6   |   1  |   9   |   1 |
  5       |  8   |   8  |   2   |   4 |

我可以用这样简单的东西得到我想要的行

Dim CleanRecord As List(Of Grower_Clean_Schedule) = (From key In eng.Grower_Clean_Schedules
                                                     Where key.Grower_Key = Grower_Key).ToList

然后我如何只返回该行的特定列的值(比如说存储在“day2”中的值)当我不知道哪个列直到运行时?

1 个答案:

答案 0 :(得分:0)

这样的事情(从您在问题中定义的CleanRecord开始):

dim matchingRow = CleanRecord.First()

dim props = matchingRow.GetType().GetProperties( _
                BindingFlags.Instance or BindingFlags.Public))

dim myReturnVal = (from prop in props _
                   where prop.Name = "day2" _
                   select prop.GetValue(matchingRow, Nothing).FirstOrDefault()

return myReturnVal
相关问题