title: Why is my Linq query on a List Dictionary throwing an "unable to cast object' error?
body: <p>I have a dictionary of type <code>Dim dictionaryOfAlertsForSC As New Dictionary(Of String, List(Of AlertsBO))</code>. The dictionary will always have two records. I need to filter out the records based on the key. I am using Linq on the dictionary:</p>
  <pre><code>lstAlertsForSC = From kv As KeyValuePair(Of String, List(Of AlertsBO)) In dictionaryOfAlertsForSC Where kv.Key.Contains(ASSESSMENTS) Select kv.Value
  </code></pre>
  <p>Where lstAlertForSC is a list of type AlertBo. It's throwing some runtime error of type casting for the select part in the Linq query. Details of exception:</p>
  <pre><code>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]'.
  </code></pre>
  <p>Here is my detailed code:</p>
  <pre><code> 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(&quot;----------------------ISP Action based Alert Management process for SC started----------------------&quot;, TraceEventType.Information, MethodBase.GetCurrentMethod(), LoggingHelper.ISPProcesses.ISPAlertForSC)
          ispAlertProcessDAO = New ISPAlertProcessDAO(userInfo)
          dictionaryOfAlertsForSC = ispAlertProcessDAO.GenerateSCAlert()
          If (dictionaryOfAlertsForSC.Count &gt; 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 &gt; 0) Then
                  strAlertBOXML = ispAlertProcessDAO.GenerateXMLForActionBasedAlerts(True, lstAlertsForSC)
  </code></pre>
  <p>I am getting an exception where I have applied Linq.</p>
  <p>My application is in VB.Net, but C# will also do, if necessary.</p>