C # LINQ orderby

I have a Xelement containing several 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, he orders 10 immediately after 1.

If I want it to be in strict digital order, I am making an element for Int, is this the right way to do this, or does LINQ have a built-in extension / method?

orderby Convert.ToInt32(y.Element("BUILD_ORDER").Value)
+3
source share
4 answers

LINQ to Objects does not have a built-in conversion, but LINQ to XML:

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 are using an anonymous type instead of just picking the value you want? For instance:

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

, , BUILD_ORDER ELEMENT. , int? int BUILD_ORDER, string ELEMENT:

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

, BUILD_ORDER , .

, , .

+7

. Int32.Parse(...). - , , , .

+2

, XML , , .NET .

, int.Parse .

+2

, int.

orderby int.Parse(y.Element("BUILD_ORDER").Value)
+2

Source: https://habr.com/ru/post/1795539/


All Articles