Search code examples
c#linqxelement

orderby with LINQ


I am using LINQ and want to make a list order descending. I try it like this:

XElement Icecreams =
      new XElement("Icecreams",
      new XElement("Icecream",
      new XComment("Cherry Vanilla Icecream"),
      new XElement("Flavor", "Cherry Vanilla"),
        new XElement("Flavor", "Cherry Vanilla"),
          new XElement("Flavor", "e"),
          new XElement("Flavor", "d"),
          new XElement("Flavor", "gg"),
          new XElement("Flavor", "c"),
          new XElement("Flavor", "b"),
          new XElement("Flavor", "a"),
      new XElement("ServingSize", "Half Cup"),
      new XElement("Price", 10),
      new XElement("Nutrition",
      new XElement("TotalFat", "15g"),
      new XElement("Cholesterol", "100mg"),
      new XElement("Sugars", "22g"),
      new XElement("Carbohydrate", "23g"),
      new XElement("SaturatedFat", "9g"))));

            Icecreams.Add(
            new XElement("Icecream",
            new XComment("Strawberry Icecream"),
            new XElement("Flavor", "Strawberry"),
            new XElement("ServingSize", "Half Cup"),
            new XElement("Price", 10),
            new XElement("Nutrition",
            new XElement("TotalFat", "16g"),
            new XElement("Cholesterol", "95mg"),
            new XElement("Sugars", "22g"),
            new XElement("Carbohydrate", "23g"),
            new XElement("SaturatedFat", "10g"))));


            XElement IcecreamsList = new XElement("IcecreamsList",
               (from c in Icecreams.Elements("Icecream")
                    //where (c.Element("Price").Value == "10")
                orderby c.Element("Flavor").ToString() descending
                select c));




            Icecreams.Save(@"D:/IcecreamsNiceok.xml");

But if I look at the file, then I still see this:

?xml version="1.0" encoding="utf-8"?>
<Icecreams>
  <Icecream>
    <!--Cherry Vanilla Icecream-->
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>gg</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>15g</TotalFat>
      <Cholesterol>100mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>9g</SaturatedFat>
    </Nutrition>
  </Icecream>
  <Icecream>
    <!--Strawberry Icecream-->
    <Flavor>Strawberry</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>16g</TotalFat>
      <Cholesterol>95mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>10g</SaturatedFat>
    </Nutrition>
  </Icecream>
</Icecreams>

So the element with name Flavor is nor ordered.

So my question is: how to make the list ordered descending?

Thank you.

oke, if I do it like this:

       XElement Icecreams =
  new XElement("Icecreams",
  new XElement("Icecream",
  new XComment("Cherry Vanilla Icecream"),
  new XElement("Flavor", "Cherry Vanilla"),
    new XElement("Flavor", "Cherry Vanilla"),
      new XElement("Flavor", "f"),
      new XElement("Flavor", "e"),
      new XElement("Flavor", "d"),
      new XElement("Flavor", "c"),
      new XElement("Flavor", "b"),
      new XElement("Flavor", "a"),
  new XElement("ServingSize", "Half Cup"),
  new XElement("Price", 10),
  new XElement("Nutrition",
  new XElement("TotalFat", "15g"),
  new XElement("Cholesterol", "100mg"),
  new XElement("Sugars", "22g"),
  new XElement("Carbohydrate", "23g"),
  new XElement("SaturatedFat", "9g"))));

        Icecreams.Add(
        new XElement("Icecream",
        new XComment("Strawberry Icecream"),
        new XElement("Flavor", "Strawberry"),
        new XElement("ServingSize", "Half Cup"),
        new XElement("Price", 10),
        new XElement("Nutrition",
        new XElement("TotalFat", "16g"),
        new XElement("Cholesterol", "95mg"),
        new XElement("Sugars", "22g"),
        new XElement("Carbohydrate", "23g"),
        new XElement("SaturatedFat", "10g"))));


        XElement IcecreamsList = new XElement("IcecreamsList",
           (from c in Icecreams.Elements("Icecream")
                //where (c.Element("Price").Value == "10")
            orderby c.Element("Flavor").ToString() ascending
            select c));


        IcecreamsList.Save(@"D:/IcecreamlistOrdered.xml");

So I save the ordered list.The output is still the same:

<?xml version="1.0" encoding="utf-8"?>
<IcecreamsList>
  <Icecream>
    <!--Cherry Vanilla Icecream-->
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>Cherry Vanilla</Flavor>
    <Flavor>f</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>15g</TotalFat>
      <Cholesterol>100mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>9g</SaturatedFat>
    </Nutrition>
  </Icecream>
  <Icecream>
    <!--Strawberry Icecream-->
    <Flavor>Strawberry</Flavor>
    <ServingSize>Half Cup</ServingSize>
    <Price>10</Price>
    <Nutrition>
      <TotalFat>16g</TotalFat>
      <Cholesterol>95mg</Cholesterol>
      <Sugars>22g</Sugars>
      <Carbohydrate>23g</Carbohydrate>
      <SaturatedFat>10g</SaturatedFat>
    </Nutrition>
  </Icecream>
</IcecreamsList>

Because I do ascending, it has to be:

a b c ..

So if I try it like this:

XElement IcecreamsList = new XElement("IcecreamsList",
               (from c in Icecreams.Elements("Icecream")
                    //where (c.Element("Price").Value == "10")
                orderby c.Element("Flavor").Value ascending
                select c));

still the output is like this:

<Flavor>f</Flavor>
    <Flavor>e</Flavor>
    <Flavor>d</Flavor>
    <Flavor>c</Flavor>
    <Flavor>b</Flavor>
    <Flavor>a</Flavor>

I have it like this:

      new XElement("Flavor", "f"),
      new XElement("Flavor", "e"),
      new XElement("Flavor", "d"),
      new XElement("Flavor", "c"),
      new XElement("Flavor", "b"),
      new XElement("Flavor", "a"),

but after I save the file. I want it to have it in ascending order, like this:

new XElement("Flavor", "f"),
          new XElement("Flavor", "a"),
          new XElement("Flavor", "b"),
          new XElement("Flavor", "c"),
          new XElement("Flavor", "d"),
          new XElement("Flavor", "e"),

Solution

  • Your problem is that you are trying to Order By Flavor value the Icecreams and not the Flavors. Here is example how you can order Flawors in the Icecreams:

    foreach(XElement iceCream in Icecreams.Elements("Icecream"))
    {
        XElement IcecreamsList = new XElement("IcecreamsList",
                (from c in iceCream.Elements("Flavor")
                orderby c.Value ascending
                select c));
        for (int i = 0; i < iceCream.Elements("Flavor").Count();i++)
        {
            iceCream.Elements("Flavor").ToList()[i].ReplaceWith(IcecreamsList.Elements("Flavor").ToList()[i]);
        }
    }
    
    Icecreams.Save(YOUR_PATH);