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

  • SEARCH
  • Home
  • 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 6340359
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T19:49:12+00:00 2026-05-24T19:49:12+00:00

Trying to create linq query from following xml. <root> <mileage value=100> </mileage> <mileage value=75>

  • 0

Trying to create linq query from following xml.

<root>
  <mileage value="100">
  </mileage>
  <mileage value="75">
  </mileage>
  <mileage value="60">
  </mileage>
   <mileage value="50">
  </mileage>
</root>

The output needs to iterate xml tree getting all mileage values in mileage node and count each total when greater than or equal to 50, greater than or equal to 75 and greater than or equal to 100. So the output would be:

<root>
      <mileage value="100" count="1"/>
      <mileage value="75" count="2"/>
      <mileage value="50" count="4"/>
</root>

Not sure where to begin

  • 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-24T19:49:13+00:00Added an answer on May 24, 2026 at 7:49 pm

    Here’s a piece of code that does exactly what you want. You should add error handling and consider the cases where not all the attributes are present or they are not what you expect (mileage value is not an int for example).

        static void Main(string[] args)
        {
            XElement result = new XElement("root");
            int[] groupValues = { 100, 75, 50 };
    
            string xml = "<root><mileage value=\"100\"></mileage><mileage value=\"75\"></mileage><mileage value=\"60\"></mileage><mileage value=\"50\"></mileage></root>";
            XElement element = XElement.Parse(xml);
    
            foreach (int groupValue in groupValues)
            {
                var newGroup = new XElement("mileage");
                newGroup.Add(new XAttribute("value", groupValue));
                newGroup.Add(new XAttribute("count", element.Elements("mileage").Count(m => int.Parse(m.Attribute("value").Value) >= groupValue)));
    
                result.Add(newGroup);
            }
    
            Console.WriteLine(result.ToString());
            Console.ReadKey();
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to create Linq Expression that I can use to query child
I am trying to create a linq 2 sql in EF 4.0 query like
I'm trying to create a LINQ query (or queries) that count the total number
I'm using LINQ-to-Entities. Using the following query: var x = from u in context.Users
I am trying to create a LINQ to SQL query and am really stumped.
Is there a better way to create the following linq to sql query? I
I have the following LINQ query that returns two objects from my database. These
I'm trying to create a Linq query for EF that joins on 2 values
I'm trying to create a query in c# for Windows Mobile 7 using linq
I'm trying to create a Linq query based on a user's input. Writing sql

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.