我正在尝试读取一个XML文件,但由于以下查询得到了type of the expression in the select clause is incorrect. Type inference failed in the call to 'Select'错误:
List<Data> dogs = (from q in doc.Descendants("dog")
where (string)q.Attribute("name") == dogName
select new Data
{
name = q.Attribute("name").Value,
breed = q.Element("breed").Value,
sex = q.Element("sex").Value
}.ToList<Data>);数据类:
public class Data
{
public string name { get; set; }
public string breed { get; set; }
public string sex { get; set; }
public List<string> dogs { get; set; }
}发布于 2013-07-25 15:07:35
问题在于您的结束括号-在ToList()调用的末尾,当您打算将它放在对象初始化器的末尾时。而且,您实际上并不是在调用该方法--您只是指定了一个方法组。最后,您可以让类型推断为您计算类型参数:
List<Data> dogs = (from q in doc.Descendants("dog")
where (string)q.Attribute("name") == dogName
select new Data
{
name = q.Attribute("name").Value,
breed = q.Element("breed").Value,
sex = q.Element("sex").Value
}).ToList();https://stackoverflow.com/questions/17861740
复制相似问题