使用 LINQ 排序

orderby with LINQ

我正在使用 LINQ 并希望按降序排列列表顺序。我这样试:

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");

但是如果我查看文件,我仍然会看到:

?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>

因此名称为 Flavor 的元素未排序。

所以我的问题是:如何使列表按降序排列?

谢谢。

好的,如果我这样做的话:

       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");

所以我保存顺序list.The输出还是一样的:

<?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>

因为我做升序,所以必须是:

一个 b C ..

所以如果我这样尝试:

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));

输出仍然是这样的:

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

我是这样的:

      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("Flavor", "f"),
          new XElement("Flavor", "a"),
          new XElement("Flavor", "b"),
          new XElement("Flavor", "c"),
          new XElement("Flavor", "d"),
          new XElement("Flavor", "e"),

您按口味订购了冰淇淋

因此,"f" 口味的冰淇淋位于 "Strawberry" 口味的冰淇淋之上。我会说这是按预期工作的。与代码相比,我看不出结果有什么问题。

也许你的期望落空了。你的意思是要订购每个冰淇淋元素中的口味吗?

您的问题是您试图按 FlavorIcecreams 而不是 Flavors 进行排序。 以下是如何在 Icecreams:

中订购 Flawors 的示例
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);