无法转换类型为'WhereSelectEnumerableIterator的对象 [英] Unable to cast object of type 'WhereSelectEnumerableIterator
问题描述
我正在尝试将xml结果转换为给出此错误的IQueryable:
I am trying to cast my xml result to an IQueryable which gives this error:
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]'.
如何将其转换为正确的电台转换?参见下面的代码:
How can I convert this to get the right stations conversion? see code below:
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; }
}
}
推荐答案
您应该将foreach
集成到LINQ查询中:
You should integrate your foreach
into LINQ query:
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>
,但这在这里确实没有任何意义.
But it will give you IEnumerable<Station>
not IQueryable<>
. You can get IQueryable<Station>
by calling AsQueryable()
method, but that really doesn't make any sense here.
此外,您可以直接将XElement
强制转换为DateTime
和Decimal
,而不是先强制转换为string
然后调用Parse
方法.
Also, you can cast XElement
to DateTime
and Decimal
directly, instead of casting to string
first and then calling Parse
method.
这篇关于无法转换类型为'WhereSelectEnumerableIterator的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!