简体   繁体   English

LINQ订购

[英]orderby with LINQ

I am using LINQ and want to make a list order descending. 我正在使用LINQ,想使列表顺序降序。 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. 因此,名称为Flavor的元素也不是有序的。

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: 因为我确实升序,所以它必须是:

abc .. abc ..

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

Your problem is that you are trying to Order By Flavor value the Icecreams and not the Flavors . 你的问题是,你正在试图ORDER BY FlavorIcecreams ,而不是Flavors Here is example how you can order Flawors in the Icecreams : 这是您如何在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);

You ordered your Icecreams by flavor . 您按口味订购冰淇淋

So your ice cream with the flavor "f" is on top of your ice cream with the flavor "Strawberry". 因此,具有“ f”风味的冰淇淋位于具有“草莓”风味的冰淇淋之上。 I would say that's working as expected. 我会说这按预期工作。 I cannot see anything wrong with the result compared to the code. 与代码相比,我看不出结果有什么问题。

Maybe your expectations are off. 也许您的期望落空了。 Did you instead mean to order the flavors inside each ice cream element? 您是不是要订购每个冰淇淋元素中的口味?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM