2014-03-06 99 views
2

我想投我的XML結果爲IQueryable這給這個錯誤:無法投類型的對象WhereSelectEnumerableIterator

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]'. 

我怎樣才能將它轉換爲得到正確的站轉換?看下面的代碼:

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 = @"<Observations><Observation><Station>ADELONG POST OFFICE</Station><DateTime>2010-09-02T00:00:00</DateTime><Temperature>19.212989033764689</Temperature></Observation><Observation><Station>ADELONG POST OFFICE</Station><DateTime>2010-09-01T00:00:00</DateTime><Temperature>28.529448969536205</Temperature></Observation><Observation><Station>ALBURY AIRPORT</Station><DateTime>2010-09-01T00:00:00</DateTime><Temperature>34.687027630716109</Temperature></Observation><Observation><Station>ALBURY AIRPORT AWS</Station><DateTime>2010-09-01T00:00:00</DateTime><Temperature>28.131385570453197</Temperature></Observation></Observations>"; 
      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 => DateTime.Parse((string)o.Element("DateTime"))); 
       var latestObservation = q.FirstOrDefault(o => 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<Station>)query; 


      Console.ReadLine(); 
     } 
    } 

    public class Station 
    { 
     public string Name { get; set; } 
     public DateTime MostRecentDate { get; set; } 
     public decimal LastTemperature { get; set; } 

    } 
} 
+1

由於錯誤試圖告訴你,一個LINQ查詢不是'IQuerable',並且一組分組字符串不是一個站的集合。 – SLaks

回答

1

你應該整合您foreach到LINQ查詢:

var query = from item in weatherData.Descendants("Observation") 
      group item by (string)item.Element("Station") into g 
      let maxDate = g.Max(o => (DateTime)o.Element("DateTime")) 
      let latestObservation = g.FirstOrDefault(o => (DateTime)o.Element("DateTime") == maxDate) 
      select new Station() 
      { 
       Name = g.Key, 
       MostRecentDate = maxDate, 
       LastTemperature = (decimal)latestObservation.Element("Temperature") 
      } 

但它會給你IEnumerable<Station>IQueryable<>。通過調用AsQueryable()方法,您可以獲得IQueryable<Station>,但這在這裏確實沒有任何意義。

此外,您還可以投XElement直接DateTimeDecimal,而不是鑄造string,然後再調用Parse方法。

相關問題