我正在尝试访问在其他过程中使用todictionary
方法创建的字典。
这是我想要的,但我收到编译错误。
Public dctOPT As New Dictionary(Of Integer, Object)
Sub Get_RTD_OPT()
Dim lstOPT = From C In DATA.CHAINs, O In DATA.OPTIONs, U In DATA.UDLies
Where C.CONTRACT = O.CONTRACT And O.UDLY_SYM = U.UDLY_SYM
Select C.ID, C.P_C, C.STRIKE, C.CONTRACT,
O.OPT_EXCHANGE, O.OPT_EXPIRY, O.OPT_SEC_TYPE, O.SYM,
U.CURRENCY
dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID)
end sub
这会编译,但在其他程序中我无法访问dctOPT
。
dim dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID)
我是否需要将dctOPT
复制到另一个公共字典中,还是有更简单的方法?有什么建议吗?
答案 0 :(得分:1)
你需要这个:
Public dctOPT As New Dictionary(Of Integer, Object)
Sub Get_RTD_OPT()
Dim lstOPT =
From C In DATA.CHAINs, O In DATA.OPTIONs, U In DATA.UDLies
Where C.CONTRACT = O.CONTRACT And O.UDLY_SYM = U.UDLY_SYM
Select C.ID, C.P_C, C.STRIKE, C.CONTRACT,
O.OPT_EXCHANGE, O.OPT_EXPIRY, O.OPT_SEC_TYPE, O.SYM,
U.CURRENCY
dctOPT = lstOPT.ToDictionary(Function(Z) Z.ID, Function(Z) CType(Z, Object))
End Sub
如果没有Function(Z) CType(Z, Object)
,则创建的字典类型为Dictionary(Of Integer, <anonymous>)
,并且无法直接分配给Dictionary(Of Integer, Object)
。强制转换正确生成了正确类型的字典。