[英]c# trying to convert IEnumerable<XElement> into a List<string>
我只是不知道如何从IEnumerable转换为列表
public IEnumerable<XElement> GetProjects()
{
return xd.Element("root").Element("projects").Elements();
}
这会给我错误
public List<string> Projects
{
get { return GetProjects().ToList(); }
set { Projects_ = value; }
}
Cannot implicitly convert type 'System.Collections.Generic.List<System.Xml.Linq.XElement>' to 'System.Collections.Generic.List<string>'
我可以添加.Select,但是我觉得这可能是一种更简单的方法。 令人赞赏的提示!
只需使用.Select:
public IEnumerable<string> GetProjects()
{
return xd.Element("root").Element("projects").Elements().Select(x => x.Value);
}
Enumerable.Select
几乎是您最简单的方法。 请参见以下示例:
public List<string> Convert(IEnumerable<XElement> items) {
return items.Select(item => item.ToString()).ToList();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.