Linq 查询转换为 List;
我有这段代码
List<string> IDs = new List<string>();
XDocument doc = XDocument.Parse(xmlFile);
var query = from c in doc.Root.Elements("a").Elements("b")
select new { ID = c.Element("val").Value};
如何将查询转换为列表而不循环 foreach ?
{ ID = c.Element("val")}
当然是字符串
编辑
我的XML文件
<?xml version="1.0" encoding="utf-8"?>
<aBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<a>
<b>
<val>other data</val>
</b>
<b>
<val>other data</val>
</b>
</a>
</aBase>
I have this code
List<string> IDs = new List<string>();
XDocument doc = XDocument.Parse(xmlFile);
var query = from c in doc.Root.Elements("a").Elements("b")
select new { ID = c.Element("val").Value};
How can I convert query to List without loop foreach ?
{ ID = c.Element("val")}
are strings of course
EDIT
my XML File
<?xml version="1.0" encoding="utf-8"?>
<aBase xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<a>
<b>
<val>other data</val>
</b>
<b>
<val>other data</val>
</b>
</a>
</aBase>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者如果你想用一行来做
or if you'd like to do it in one line
匿名类型并不能真正帮助您,因为您只需要一系列字符串,而不是任何类型的元组。尝试:
就我个人而言,我会一直使用方法语法:
The anonymous type isn't really helping you since you only need a sequence of strings, not any sort of tuple. Try:
Personally, I would just use method-syntax all the way: