Linq to XML 查询在返回单个实体时有效,但需要是列表
给定这个 XML:
<foo>
<xxx>Some XXX</xxx>
<bar Attr1="true">Some Bar</bar>
<bar Attr2="true">Some Bar #2</bar>
<bar>Some Bar #3</bar>
<bar>Some Bar #4</bar>
</foo>
我有这个 LINQ 查询:
var foos = from query in xmlData.Descendants("foo")
from bars in query.Elements("bar")
select new FooClass
{
xxx = (string)query.Element("xxx"),
barCollection = new BarClass
{
display = bars.Value,
attr1Exists = (answers.Attribute("Attr1") != null ? true : false)
attr2Exists = (answers.Attribute("Attr2") != null ? true : false)
}
};
如果将 barCollection 定义为 BarClass,则效果很好。
不过我需要它是 List
我尝试过其他方法,如果 bar 是 List
var foos = from query in xmlData.Descendants("foo")
select new FooClass
{
xxx = (string)query.Element("xxx"),
bar = query.Elements("bar").Select(x => x.Value).ToList()
};
但我找不到混合两者的方法。
有人可以展示如何组合这两个解决方案,以便将 BarCollection 填充为用户定义的类型,即 List
更新:下面的最终工作代码,使用提供的解决方案
var foos = from query in xmlData.Descendants("foo")
select new FooClass
{
xxx = (string)query.Element("xxx"),
bar = query.Elements("bar").Select(x => new BarClass
{
display = x.Value,
attr1Exists = x("Attr1") != null ? true : false),
attr2Exists = x("Attr2") != null ? true : false)
}
).ToList()
};
Given this XML:
<foo>
<xxx>Some XXX</xxx>
<bar Attr1="true">Some Bar</bar>
<bar Attr2="true">Some Bar #2</bar>
<bar>Some Bar #3</bar>
<bar>Some Bar #4</bar>
</foo>
I have this LINQ query:
var foos = from query in xmlData.Descendants("foo")
from bars in query.Elements("bar")
select new FooClass
{
xxx = (string)query.Element("xxx"),
barCollection = new BarClass
{
display = bars.Value,
attr1Exists = (answers.Attribute("Attr1") != null ? true : false)
attr2Exists = (answers.Attribute("Attr2") != null ? true : false)
}
};
This works great if barCollection is defined as BarClass.
However I need it to be List<BarClass>.
I've tried other approaches, and got this to work if bar is a List<string>:
var foos = from query in xmlData.Descendants("foo")
select new FooClass
{
xxx = (string)query.Element("xxx"),
bar = query.Elements("bar").Select(x => x.Value).ToList()
};
But I can't find a way to mix the two.
Can someone show how to combine the two solutions so that BarCollection is populated as a user defined type, namely List<BarClass>?
Update: Final working code below, using solutions provided
var foos = from query in xmlData.Descendants("foo")
select new FooClass
{
xxx = (string)query.Element("xxx"),
bar = query.Elements("bar").Select(x => new BarClass
{
display = x.Value,
attr1Exists = x("Attr1") != null ? true : false),
attr2Exists = x("Attr2") != null ? true : false)
}
).ToList()
};
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
.Select(x => x.Value)
更改为.Select(x => new BarClass(...))
。Change
.Select(x => x.Value)
to.Select(x => new BarClass(...))
.所以......稍微改一下你的问题......
你想要一个 Foo 类,它有一个 BarClasses 集合作为其属性之一?
如果这是正确的,那么您不需要选择很多(您的第二个 from 子句),
您会做几乎与第二种方法完全相同的事情......
So...rephrasing your question slightly....
you want a Foo class that has a Collection of BarClasses as one of its properties?
If this is correct, then you don't need a select many (your second from clause)
you would do something almost exactly like your second approach....