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 4016236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:42:56+00:00 2026-05-20T09:42:56+00:00

I have an Xelement containing a number of elements. I have the following code

  • 0

I have an Xelement containing a number of elements.

I have the following code to sort them:

var calculation = from y in x.Elements("row")
                 orderby y.Element("BUILD_ORDER").Value
                 select new
                 {
                     calcAttribute = y.Element("ELEMENT").Value

                 };

Which works fine, until BUILD_ORDER > 10, it orders 10 just after 1.

If I want it in strict numerical order, I case the element to an Int, is this the correct way to do it, or does LINQ have an inbuilt extension/method?

orderby Convert.ToInt32(y.Element("BUILD_ORDER").Value)
  • 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-20T09:42:56+00:00Added an answer on May 20, 2026 at 9:42 am

    LINQ to Objects doesn’t have the built in conversion, but LINQ to XML does:

    var calculation = from y in x.Elements("row")
                      orderby (int) y.Element("BUILD_ORDER")
                      select new
                      {
                          calcAttribute = y.Element("ELEMENT").Value
                      };
    

    Is there any reason why you’re using an anonymous type though, rather than just selecting the value you want? For example:

    var calculation = from y in x.Elements("row")
                      orderby (int) y.Element("BUILD_ORDER")
                      select y.Element("ELEMENT").Value;
    

    Note that both of these will throw an exception if the BUILD_ORDER or ELEMENT subelements are missing. You can fix that using a conversion to int? instead of int for BUILD_ORDER, and a conversion to string for ELEMENT:

    var calculation = from y in x.Elements("row")
                      orderby (int?) y.Element("BUILD_ORDER")
                      select (string) y.Element("ELEMENT");
    

    This will still fail if BUILD_ORDER exists but can’t be parsed as an integer of course.

    If your data should always have these elements, the first form is better, as it’ll detect any data errors earlier.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Suppose I have the following code: var X = XElement.Parse (@ <ROOT> <MUL v='2'
I have got this code: XDocument xdoc = XDocument.Load(URI); XElement root = xdoc.Element(forecast); //get
I have the following query: Dim elementsWithPossibleCCNumbers As IEnumerable(Of XElement) = xmlTree.Descendants(). Where(Function(element) element.Attributes().
I have the following XML Parsing code in my application: public static XElement Parse(string
EDIT: Switched from XmlTextWriter to XElement I have a Player class, which has following
I have an XElement and I need to add more elements to it from
So I have the following code: public static void Replace(filepath) { try { XElement
I have an XElement object which has a number of attributes and I simply
If I have an XElement that has child elements, and if I remove a
If I have the following xml: XDocument xDocument = new XDocument( new XElement(RootElement, new

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.