Sort and group XML Based on Attributes

2019-06-01 16:28发布

问题:

I am working with LINQ to XML in C# and I need to do some sorting on the attributes of my xml nodes. My xml looks something like this.

<root> 
    <Claim key="1" carrier="carA" zip="34343" pages="1"/>
    <Claim key="2" carrier="carA" zip="34343" pages="2"/>
    <Claim key="3" carrier="carB" zip="34343" pages="4"/>
</root>

I can sort the xml using the orderby clause such as

 var query= from claim in root.Elements("Claim")
            let Key = claim.Attributes("Key").First().Value
            let Carrier = claim.Attributes("Carrier").First().Value
            let CarrierZip = int.Parse(claim.Attributes("CarrierZip").First().Value)
            let Pages = claim.Attributes("Pages").First().Value
            orderby Pages ascending, CarrierZip ascending, Carrier ascending
            select claim;

Then I get a list of the keys from query.

What I want to do is get collections of all the claims with 1 page, then all the claims with 2 pages and so on, but I do not know what the maximum number of pages can be.

can anyone help me out?

EDIT -

I changed my intial thought on how to accomplish this and I now would like the out put to resemble this.

List<List<List<List<int>>>>

All claims 
- 1 page
   -zip1
      -carr1
         -int claim key
         -int claim2 key
      - car2
   -zip2
      -car1
- 2 pages 
   -zip1

and so on. The trick is I need to query the nodes and get multiple groups out of it. Can this be done in my statement or is a series of statements required?

回答1:

You just need to add a GroupBy clause:

var query= from claim in root.Elements("Claim")
           let Key = claim.Attributes("Key").First().Value
           let Carrier = claim.Attributes("Carrier").First().Value
           let CarrierZip = int.Parse(claim.Attributes("CarrierZip").First().Value)
           let Pages = claim.Attributes("Pages").First().Value
           orderby Pages ascending, CarrierZip ascending, Carrier ascending
           group new { CarrierZip, Carrier, Key } by Pages;

foreach (var group in query)
{
    Console.WriteLine("Claims with {0} pages:", group.Key);
    foreach (var entry in group)
    {
        Console.WriteLine("  {0} {1} {2}", entry.CarrierZip, entry.Carrier,
                          entry.Key);
    }
}

EDIT: To get a List<List<int>> from this, you'd use:

var claims = query.Select(group => group.Select(x => x.Key).ToList())
                  .ToList();

Alternatively, if you don't need the CarrierZip and Carrier in the results, you can simplify the query and the listifying to:

var query= from claim in root.Elements("Claim")
           let Key = claim.Attributes("Key").First().Value
           let Carrier = claim.Attributes("Carrier").First().Value
           let CarrierZip = int.Parse(claim.Attributes("CarrierZip").First().Value)
           let Pages = claim.Attributes("Pages").First().Value
           orderby Pages ascending, CarrierZip ascending, Carrier ascending
           group Key by Pages;

var claims = query.Select(group => group.ToList())
                  .ToList();