无法将类型为“WhereSelectEnumerableIterator”的对象转换为类型

4

我正在尝试将我的xml结果转换为IQueryable,但出现了以下错误:

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个回答

2

您应该将 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`转换为`DateTime`和`Decimal`,而不是先转换为`string`,然后再调用`Parse`方法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接