English 中文(简体)
帮助LINQ到Xml
原标题:Help with LINQ to Xml

我在前面张贴了另一个关于如何改变与《国际公路货运公约》的XmlDocument的职位,但也许有可能与《路运危险货物协定》打交道。

我有一份XmlDocument,研究的是:

<DocumentElement>
  <Customer>
    <CustomerId>2315</CustomerId>
    <VersionNumber>1</VersionNumber>
    <GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
  </Customer>
  <Customer>
    <CustomerId>2316</CustomerId>
    <VersionNumber>2</VersionNumber>
    <GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
  </Customer>
  <Customer>
    <CustomerId>2317</CustomerId>
    <VersionNumber>1</VersionNumber>
    <GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
  </Customer>
</DocumentElement>

有了LINQ,我能否为每个独一无二的GUID提取一个客户要素,并使客户获得最高数字?

一、即新文件/文件将考虑:

<DocumentElement>
  <Customer>
    <CustomerId>2316</CustomerId>
    <VersionNumber>2</VersionNumber>
    <GUID>2E05DE20-02A0-425D-944D-65E5E744FF8A</GUID>
  </Customer>
  <Customer>
    <CustomerId>2317</CustomerId>
    <VersionNumber>1</VersionNumber>
    <GUID>9995DE20-02A0-425D-944D-65E5E744FF8A</GUID>
  </Customer>
</DocumentElement>

提前感谢。

最新情况:

因此,我应该做(如果我正确理解你的话):

    XDocument xdoc = new XDocument();
    using (StringWriter sw = new StringWriter())
    {
        // Converts the datatable to XML                             
        dt.WriteXml(sw);
        xdoc = XDocument.Parse(sw.ToString());
    }

        var query = xdoc.Root
            .Elements("Customer")
            .GroupBy(x => x.Element("GUID").Value)
            .Select(g => g.OrderByNumberDescending(x =>(int)x.Element("VersionNumber"))
                          .First());

不过,我还是把错误带上了B组,或者我在这里做一些其他错误?

最佳回答

绝对:

var query = parent
    .Elements("Customer")
    .GroupBy(x => x.Element("GUID").Value)
    .Select(g => g.OrderByNumberDescending(x => (int) x.Element("VersionNumber"))
                  .First());

Or using MoreLINQ:

var query = parent
     .Elements("Customer")
     .GroupBy(x => x.Element("GUID").Value)
     .Select(g => g.MaxBy(x => (int) x.Element("VersionNumber")));
问题回答

暂无回答




相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

How to Add script codes before the </body> tag ASP.NET

Heres the problem, In Masterpage, the google analytics code were pasted before the end of body tag. In ASPX page, I need to generate a script (google addItem tracker) using codebehind ClientScript ...

Transaction handling with TransactionScope

I am implementing Transaction using TransactionScope with the help this MSDN article http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx I just want to confirm that is ...

System.Web.Mvc.Controller Initialize

i have the following base controller... public class BaseController : Controller { protected override void Initialize(System.Web.Routing.RequestContext requestContext) { if (...

Microsoft.Contracts namespace

For what it is necessary Microsoft.Contracts namespace in asp.net? I mean, in what cases I could write using Microsoft.Contracts;?

Separator line in ASP.NET

I d like to add a simple separator line in an aspx web form. Does anyone know how? It sounds easy enough, but still I can t manage to find how to do it.. 10x!

热门标签