用LINQ命令

时间:2017-07-17 07:55:22

标签: c# linq xelement

我正在使用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的元素也未被排序。

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

谢谢。

oke,如果我这样做的话:

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

所以我保存了有序列表。输出仍然是相同的:

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

2 个答案:

答案 0 :(得分:2)

您的问题是,您尝试按import <package-name>排序Flavor而不是Icecreams。 以下是您可以在Flavors

中订购Flawors的示例
Icecreams

答案 1 :(得分:1)

您通过风味订购了 Icecreams

所以你的冰淇淋口味“f”就在你的冰淇淋上面,带有“草莓味”。我会说这是按预期工作的。与代码相比,我看不出结果有什么问题。

也许你的期望已经过去了。您是否意味着要在每个冰淇淋元素中订购口味?