我有一個字典 Dim dictionaryOfAlertsForSC As New Dictionary(Of String, List(Of AlertsBO))
。現在字典總是有兩個記錄。我需要根據密鑰過濾出記錄。我在字典上使用LinqLinq查詢字典的類型列表
lstAlertsForSC = From kv As KeyValuePair(Of String, List(Of AlertsBO)) In dictionaryOfAlertsForSC Where kv.Key.Contains(ASSESSMENTS) Select kv.Value
其中lstAlertForSC是類型AlertBo的列表。它在Linq查詢中爲選擇部分拋出一些類型轉換的運行時錯誤。異常詳情:Unable to cast object of type 'WhereSelectEnumerableIterator
2 [System.Collections.Generic.KeyValuePair 2[System.String,System.Collections.Generic.List
1 [MA.DMR.HCSIS.Common.AlertsBO]],MA.DMR.HCSIS.Common.AlertsBO]'to type'System.Collections.Generic.List 1[MA.DMR.HCSIS.Common.AlertsBO]'.
這裏是我的詳細代碼:
Public Sub GenerateSCAlert(ByVal userInfo As UserInfoType)
Dim ispAlertProcessDAO As ISPAlertProcessDAO
Dim lstAlertsForSC As New List(Of AlertsBO)
Dim dictionaryOfAlertsForSC As New Dictionary(Of String, List(Of AlertsBO))
Dim strAlertBOXML As String
strAlertBOXML = String.Empty
Try
LoggingHelper.Log("----------------------ISP Action based Alert Management process for SC started----------------------", TraceEventType.Information, MethodBase.GetCurrentMethod(), LoggingHelper.ISPProcesses.ISPAlertForSC)
ispAlertProcessDAO = New ISPAlertProcessDAO(userInfo)
dictionaryOfAlertsForSC = ispAlertProcessDAO.GenerateSCAlert()
If (dictionaryOfAlertsForSC.Count > 0) Then
lstAlertsForSC = From kv As KeyValuePair(Of String, List(Of AlertsBO)) In dictionaryOfAlertsForSC Where kv.Key.Contains(ASSESSMENTS) Select kv.Value.Single()
If (lstAlertsForSC.Count > 0) Then
strAlertBOXML = ispAlertProcessDAO.GenerateXMLForActionBasedAlerts(True, lstAlertsForSC)
我正在那裏我已經申請的LINQ
這將是非常有益的,如果有人能幫我出這個例外。
我有我的應用程序在vb.net,所以它會有幫助,如果我在vb.net得到答案。但是c#也會這樣。
在此先感謝............
看來你正在提取一個項目。然後你可以做'=(從...選擇kv.Value).Single()'。 –
感謝您的回覆。我試過你的解決方案,但它給了我同樣的錯誤。(增加了錯誤描述)。基本上從一個類型的字典(字符串,列表(類型的對象))我需要獲取某個鍵的列表。如果您有其他解決方法,這將非常有幫助。 – sudipBanerjee
更確切地說,我希望該值作爲類型(字符串,列表(類型對象))的字典(鍵值對)中對應鍵的列表...... – sudipBanerjee