我有一个 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>
如何按作者、书名或年份对书籍进行排序?谢谢
你想要_____吗read(查询)特定顺序的数据,还是您实际上想要对 xml 中的数据重新排序?要按特定顺序读取,只需使用 LINQOrderBy
method:
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);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)