Linq to XML 查询在返回单个实体时有效,但需要是列表

发布于 2024-10-10 21:38:38 字数 1634 浏览 2 评论 0原文

给定这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

怪我闹别瞎闹 2024-10-17 21:38:38

.Select(x => x.Value) 更改为 .Select(x => new BarClass(...))

Change .Select(x => x.Value) to .Select(x => new BarClass(...)).

萌能量女王 2024-10-17 21:38:38

所以......稍微改一下你的问题......

你想要一个 Foo 类,它有一个 BarClasses 集合作为其属性之一?

List<BarClass> mybars = Foo.Bars; // is this your aim?

如果这是正确的,那么您不需要选择很多(您的第二个 from 子句),

您会做几乎与第二种方法完全相同的事情......

  var foos = from query in xmlData.Descendants("foo")      
  select new FooClass
  {          
       xxx = (string)query.Element("xxx"), 
       Bars = query.Elements("bar").Select(x => new BarClass()
          {
             Attr1 = .. /// etc
             Attr2 = ....
          }).ToList();
  } 

So...rephrasing your question slightly....

you want a Foo class that has a Collection of BarClasses as one of its properties?

List<BarClass> mybars = Foo.Bars; // is this your aim?

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....

  var foos = from query in xmlData.Descendants("foo")      
  select new FooClass
  {          
       xxx = (string)query.Element("xxx"), 
       Bars = query.Elements("bar").Select(x => new BarClass()
          {
             Attr1 = .. /// etc
             Attr2 = ....
          }).ToList();
  } 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文