Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7661507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:36:27+00:00 2026-05-31T13:36:27+00:00

I am working with LINQ to XML in C# and I need to do

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T13:36:27+00:00Added an answer on May 31, 2026 at 1:36 pm

    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();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working with a method using LINQ to XML to return a string.This
I've been working with the xml.linq classes in .net recently. But may need to
I am a .net beginner. I need to add some data to xml file
I'm currently working on a Silverlight app and need to convert XML data into
I am working at Windows Phone 7 C#, XAML, XML and LINQ programming. I
Working on a application which reads some values from an customers.xml file to UI.
I'm working with Linq to xml, and I save my xml in a standars
This is my first day working with XML in the context of XPath, XQuery,
I'm trying to learn some Linq to XML stuff, and I came across the
I am working with Linq to Xml to manipulate openXml documents. More precisely I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.