繁体   English   中英

C#试图转换IEnumerable <XElement> 进入列表 <string>

[英]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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM