LINQ to XML查询是否可以实现??? [英] Is this possible with a LINQ to XML query???

查看:94
本文介绍了LINQ to XML查询是否可以实现???的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在底部,我粘贴了一个正在使用的XML文件.我的目标是使用LINQ to XML查询此文件,并将响应格式设置为:

reportDate            alias24        alias39
2009-05-27        138,385.11     2,026.49
2009-05-28        138,535.98      2,233.74
2009-05-29        138,825.29      2,637.20

如您所见,我基本上只是想要并排列出的每个帐户的总计列.但是,我不确定该怎么做.我最近能得到的东西看起来像这样(0是给第一个别名的枚举值,而1是
给第二个别名的枚举值).

2009-05-27   0
2009-05-27  1
2009-05-28   0
2009-05-28  1
2009-05-29   0
2009-05-29  1

但是我不确定如何将每个日期的行合并在一起,然后列出总数.

任何LINQ to XML专家都可以帮上忙吗? br>
这是文件...
-------------------------->
< FlexQueryResponse queryName ="TestingXML" type ="AF">
< Message>该语句不包括以下帐户:U69</Message>
< FlexStatements count ="0">
< FlexStatement accountId ="U24"从日期="20090501"至日期="20090529">
< AccountInformation acctAlias ="alias24" accountId ="U24"/>
< EquitySummaryInBase< E> ; EquitySummaryByReportDateInBase reportDate ="2009-05-27" total ="138,385.11"/>
< EquitySummaryByReportDateInBase reportDate ="2009-05-28" total ="138,535.98"/>
< EquitySummaryByReportDateInBase reportDate = "2009-05-29" total ="138,825.29"/>
</EquitySummaryInBase>

</FlexStatement>< FlexStatement accountId ="U39" fromDate ="20090501" toDate ="20090529>
< Accoun tInformation acctAlias ="alias39" accountId ="U39"/>
< EquitySummaryInBase>
< EquitySummaryByReportDateInBase reportDate ="2009-05-27" total ="2,026.49"/>
< EquitySummaryByReport reportDate ="2009-05-28" total ="2,233.74"/>
< EquitySummaryByReportDateInBase reportDate ="2009-05-29" total ="2,637.20"/>
</EquitySummaryInBase>

</FlexStatement>
</FlexStatements>
</FlexQueryResponse>

At the bottom I have pasted in an XML file I am working with.   My goal is to query this file using LINQ to XML and have the response formatted as follows:

reportDate            alias24         alias39
2009-05-27         138,385.11      2,026.49
2009-05-28         138,535.98      2,233.74
2009-05-29         138,825.29      2,637.20

As you can see, I basically just want the total column for each account listed side by side. However, I''m not sure how to do this.   The closest I''ve been able to come is getting something that looks like this (with the 0 being the enumerated value given to the first alias and 1 being
the enumerated value given to the second alias).

2009-05-27   0
2009-05-27   1
2009-05-28   0
2009-05-28   1
2009-05-29   0
2009-05-29   1

But I''m not sure how I would combine the rows for each date...and then have the total listed.

Can any LINQ to XML gurus out there help?

Here''s the file...
-------------------------->
<FlexQueryResponse queryName="TestingXML" type="AF">
<Message>The following accounts were excluded from this statement: U69</Message>
<FlexStatements count="0">
<FlexStatement accountId="U24" fromDate="20090501" toDate="20090529">
<AccountInformation acctAlias="alias24" accountId="U24" />
<EquitySummaryInBase>
<EquitySummaryByReportDateInBase reportDate="2009-05-27" total="138,385.11" />
<EquitySummaryByReportDateInBase reportDate="2009-05-28" total="138,535.98" />
<EquitySummaryByReportDateInBase reportDate="2009-05-29" total="138,825.29" />
</EquitySummaryInBase>

</FlexStatement><FlexStatement accountId="U39" fromDate="20090501" toDate="20090529">
<AccountInformation acctAlias="alias39" accountId="U39" />
<EquitySummaryInBase>
<EquitySummaryByReportDateInBase reportDate="2009-05-27" total="2,026.49" />
<EquitySummaryByReportDateInBase reportDate="2009-05-28" total="2,233.74" />
<EquitySummaryByReportDateInBase reportDate="2009-05-29" total="2,637.20" />
</EquitySummaryInBase>

</FlexStatement>
</FlexStatements>
</FlexQueryResponse>

推荐答案

您应该首先进行查询以获取每个别名和日期的所有总计:
You should first make a query for getting all totals for each alias and date:
var flatteningQuery = from statement in doc.Root.Element("FlexStatements").Descendants("FlexStatement")
                      from summary in statement.Element("EquitySummaryInBase").Descendants("EquitySummaryByReportDateInBase")
                      select new
                      {
                          Alias = statement.Element("AccountInformation").Attribute("acctAlias").Value,
                          Date = summary.Attribute("reportDate").Value,
                          Total = summary.Attribute("total").Value
                      };

然后,您可以将每个日期的上一个查询的结果分组,以获取与报告日期相对应的所有别名-总计对的列表:

Then, you can group the result from the previous query for each date, getting the list of all alias-total pair corresponding to report dates:

var groupedQuery = from row in flatteningQuery
                   group row by row.Date into dateGroup
                   select new
                   {
                       Date = dateGroup.Key,
                       Values = from item in dateGroup
                                select new
                                {
                                    Alias = item.Alias,
                                    Total = item.Total
                                }
                   };


这篇关于LINQ to XML查询是否可以实现???的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆