LINQ - 获取一组元素的最大值

发布于 2024-11-01 09:56:06 字数 1844 浏览 0 评论 0原文

如何使用 LINQ 查询 XML 结构并获取属于特定元素的一组元素的最大值。

例如,如何获得 FullTicket、DayTicket 和 ChildTicket 允许的最大年龄,其中 XML 结构如下所示

    <TicketTypes>
      <TicketType Name="FullTicket">
        <AgeBands>
          <AgeBand>
            <Code>Adult1</Code>
            <MinAge>18</MinAge>
            <MaxAge>59</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult2</Code>
            <MinAge>60</MinAge>
            <MaxAge>64</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult3</Code>
            <MinAge>65</MinAge>
            <MaxAge>79</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
      <TicketType Name="DayTicket">
        <AgeBands>
          <AgeBand>
            <Code>Adult2</Code>
            <MinAge>18</MinAge>
            <MaxAge>64</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult3</Code>
            <MinAge>65</MinAge>
            <MaxAge>89</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
      <TicketType Name="ChildTicket">
        <AgeBands>
          <AgeBand>
            <Code>Child</Code>
            <MinAge>3</MinAge>
            <MaxAge>17</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Infant</Code>
            <MinAge>0</MinAge>
            <MaxAge>2</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
    </TicketTypes>

How can I use LINQ to query an XML structure and get a max value for a group of elements that belong to a particular element.

For example, how could I get the maximum age allowed for FullTicket, DayTicket and ChildTicket where the XML structure looks like the below

    <TicketTypes>
      <TicketType Name="FullTicket">
        <AgeBands>
          <AgeBand>
            <Code>Adult1</Code>
            <MinAge>18</MinAge>
            <MaxAge>59</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult2</Code>
            <MinAge>60</MinAge>
            <MaxAge>64</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult3</Code>
            <MinAge>65</MinAge>
            <MaxAge>79</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
      <TicketType Name="DayTicket">
        <AgeBands>
          <AgeBand>
            <Code>Adult2</Code>
            <MinAge>18</MinAge>
            <MaxAge>64</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Adult3</Code>
            <MinAge>65</MinAge>
            <MaxAge>89</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
      <TicketType Name="ChildTicket">
        <AgeBands>
          <AgeBand>
            <Code>Child</Code>
            <MinAge>3</MinAge>
            <MaxAge>17</MaxAge>
          </AgeBand>
          <AgeBand>
            <Code>Infant</Code>
            <MinAge>0</MinAge>
            <MaxAge>2</MaxAge>
          </AgeBand>
        </AgeBands>
      </TicketType>
    </TicketTypes>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

随遇而安 2024-11-08 09:56:06

您的意思是您想要 MaxAge 的最高值? (这里已经有“max”的事实有点令人困惑。)尝试这样的操作:

// Find the FullTicket element
var fullTicket = ticketTypes.Elements("TicketType")
                     .Where(x => (string) x.Attribute("Name") == "FullTicket")
                     .First();

// Find the maximum value of any MaxAge element within FullTicket
var maxFullTicketAge = fullTicket.Descendants("MaxAge")
                                 .Max(x => (int) x);

您可以对其他门票类型执行相同的操作,或者如果您感觉更具冒险精神,您可以执行以下操作:

var maxAges = ticketTypes
       .Elements("TicketType")
       .Select(x => new {
                  Name = (string) x.Attribute("Name"),
                  MaxMaxAge = x.Descendants("MaxAge").Max(y => (int) y)
               });

You mean you want the highest value of MaxAge? (The fact that there's already "max" here is a bit confusing.) Try something like this:

// Find the FullTicket element
var fullTicket = ticketTypes.Elements("TicketType")
                     .Where(x => (string) x.Attribute("Name") == "FullTicket")
                     .First();

// Find the maximum value of any MaxAge element within FullTicket
var maxFullTicketAge = fullTicket.Descendants("MaxAge")
                                 .Max(x => (int) x);

You can do the same for the other ticket types, or if you're feeling more adventurous you could do something like:

var maxAges = ticketTypes
       .Elements("TicketType")
       .Select(x => new {
                  Name = (string) x.Attribute("Name"),
                  MaxMaxAge = x.Descendants("MaxAge").Max(y => (int) y)
               });
芯好空 2024-11-08 09:56:06
var values = from item in TicketTypes.Descendants("TicketType")
             from agebands in item.Descendants("AgeBands")
             from ageband in agebands.Descendants("AgeBand")
select item.Attribute("Name"), ageBand.MaxAge;
var values = from item in TicketTypes.Descendants("TicketType")
             from agebands in item.Descendants("AgeBands")
             from ageband in agebands.Descendants("AgeBand")
select item.Attribute("Name"), ageBand.MaxAge;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文