title: Unable to cast object of type 'WhereSelectEnumerableIterator
body: <p>I am trying to cast my xml result to an IQueryable which gives this error:</p>
  
  <pre><code>An unhandled exception of type 'System.InvalidCastException' occurred in weatherxml.exe
  
  Additional information: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Linq.IGrouping`2[System.String,System.Xml.Linq.XElement],System.Linq.IGrouping`2[System.String,System.Xml.Linq.XElement]]' to type 'System.Linq.IQueryable`1[weatherxml.Station]'.
  </code></pre>
  
  <p>How can I convert this to get the right stations conversion? see code below:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  using System.Threading.Tasks;
  using System.Xml;
  using System.Xml.Linq;
  
  namespace weatherxml
  {
      class Program
      {
          static void Main(string[] args)
          {
              string xmlData = @"&lt;Observations&gt;&lt;Observation&gt;&lt;Station&gt;ADELONG POST OFFICE&lt;/Station&gt;&lt;DateTime&gt;2010-09-02T00:00:00&lt;/DateTime&gt;&lt;Temperature&gt;19.212989033764689&lt;/Temperature&gt;&lt;/Observation&gt;&lt;Observation&gt;&lt;Station&gt;ADELONG POST OFFICE&lt;/Station&gt;&lt;DateTime&gt;2010-09-01T00:00:00&lt;/DateTime&gt;&lt;Temperature&gt;28.529448969536205&lt;/Temperature&gt;&lt;/Observation&gt;&lt;Observation&gt;&lt;Station&gt;ALBURY AIRPORT&lt;/Station&gt;&lt;DateTime&gt;2010-09-01T00:00:00&lt;/DateTime&gt;&lt;Temperature&gt;34.687027630716109&lt;/Temperature&gt;&lt;/Observation&gt;&lt;Observation&gt;&lt;Station&gt;ALBURY AIRPORT AWS&lt;/Station&gt;&lt;DateTime&gt;2010-09-01T00:00:00&lt;/DateTime&gt;&lt;Temperature&gt;28.131385570453197&lt;/Temperature&gt;&lt;/Observation&gt;&lt;/Observations&gt;";
              XDocument weatherData = XDocument.Parse(xmlData);
  
              var query = from item in weatherData.Descendants("Observation")
                          group item by (string)item.Element("Station")
                              into g
                              select g;
  
              foreach (var q in query)
              {
                  var maxDate = q.Max(o =&gt; DateTime.Parse((string)o.Element("DateTime")));
                  var latestObservation = q.FirstOrDefault(o =&gt; DateTime.Parse((string)o.Element("DateTime")) == maxDate);
                  var newStation = new Station();
                  newStation.Name = q.Key;
                  newStation.MostRecentDate = maxDate;
                  newStation.LastTemperature = Decimal.Parse((string)latestObservation.Element("Temperature"));
  
              }
  
              var stations=(IQueryable&lt;Station&gt;)query;
  
  
              Console.ReadLine();
          }
      }
  
      public class Station
      {
          public string Name { get; set; }
          public DateTime MostRecentDate { get; set; }
          public decimal LastTemperature { get; set; }
  
      }
  }
  </code></pre>