Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 对元素进行排序_.net_Linq - Fatal编程技术网

.net 对元素进行排序

.net 对元素进行排序,.net,linq,.net,Linq,我有一个XElement映射如下: <book> <author>sadfasdf</author> <title>asdfasdf</title> <year>1999</year> </book> <book> <author>asdfasdf</author> <title>asdfasdf</titl

我有一个XElement映射如下:

<book>
    <author>sadfasdf</author>
    <title>asdfasdf</title>
    <year>1999</year>
</book>
<book>
    <author>asdfasdf</author>
    <title>asdfasdf</title>
    <year>1888</year>
</book>
<book>
    <author>asdfsdf</author>
    <title>asdfasdf</title>
    <year>1777</year>
</book>

萨达法斯德
asdfasdf
1999
asdfasdf
asdfasdf
1888
asdfsdf
asdfasdf
1777
如何按作者、书名或年份对书籍进行排序?谢谢

您是想按特定顺序读取(查询)数据,还是真的想在xml中重新排序数据?要按特定顺序阅读,只需使用LINQ
OrderBy
方法:

    var qry = from book in el.Elements("book")
              orderby (int)book.Element("year")
              select new
              {
                  Year = (int)book.Element("year"),
                  Title = (string)book.Element("title"),
                  Author = (string)book.Element("author")
              };
(编辑)更改xml更为棘手。。。可能是这样的:

    var qry = (from book in el.Elements("book")
               orderby (int)book.Element("year")
               select book).ToArray();

    foreach (var book in qry) book.Remove();
    foreach (var book in qry) el.Add(book);

这是可行的,但有点奇怪:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;

class Test
{
    static void Main()
    {
        string xml = 
@"<books>
  <book>
    <author>sadfasdf</author>
    <title>asdfasdf</title>
    <year>1999</year>
  </book>
  <book>
    <author>asdfasdf</author>
    <title>asdfasdf</title>
    <year>1888</year>
  </book>
  <book>
    <author>asdfsdf</author>
    <title>asdfasdf</title>
    <year>1777</year>
  </book>
</books>";
        XElement root = XElement.Parse(xml);

        List<XElement> ordered = root.Elements("book")
            .OrderBy(element => (int)element.Element("year"))
            .ToList();

        root.ReplaceAll(ordered);
        Console.WriteLine(root);
    }
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Xml.Linq;
课堂测试
{
静态void Main()
{
字符串xml=
@"
萨达法斯德
asdfasdf
1999
asdfasdf
asdfasdf
1888
asdfsdf
asdfasdf
1777
";
XElement root=XElement.Parse(xml);
列表顺序=根元素(“书本”)
.OrderBy(element=>(int)element.element(“年”)
.ToList();
root.ReplaceAll(已订购);
控制台写入线(根);
}
}

请注意,如果根节点下有其他内容,则应在添加它们之前对每个
XElement
调用
Remove
,而不是只调用
RemoveAll

我只想对其重新排序。你能提供一个真实世界的例子吗?该死,我只是在打这个。。。我被飞碟剪了!请注意,对其排序的键只解析一次。在您的示例中,我担心在快速排序中,每次比较都会调用int.Parse(yearString)两次,但看起来键是由EnumerableSorter.ComputeKeys()预先解析的。