在日期时间之后对 IE 可排序
本文关键字:IE 排序 之后 日期 时间 | 更新日期: 2023-09-27 18:30:57
我有以下内容
var path = HttpContext.Current.Server.MapPath("~/App_Data/WorldNews.xml");
var feedXml = XDocument.Load(path);
IEnumerable<NewsItem> feeds = from feed in feedXml.Descendants("item")
select new NewsItem
{
Title = feed.Element("title").Value,
Link = feed.Element("link").Value,
Description = Regex.Match(feed.Element("description").Value, @"^.{1,180}'b(?<!'s)").Value,
Date = Convert.ToDateTime(feed.Element("pubDate").Value)
};
如何在"日期"之后对其进行排序?
您可以使用
let
来存储转换后的日期。
IEnumerable<NewsItem> feeds =
from feed in feedXml.Descendants("item")
let date = Convert.ToDateTime(feed.Element("pubDate").Value)
orderby date
select new NewsItem
{
Title = feed.Element("title").Value,
Link = feed.Element("link").Value,
Description = Regex.Match(feed.Element("description").Value, @"^.{1,180}'b(?<!'s)").Value,
Date = date
};
要在创建后对其进行排序,您可以执行此操作。
feeds = feeds.OrderBy(f => f.Link).ToArray();
我添加.ToArray()
的原因是,如果没有它,您将实例化一系列查询,每个查询的计算成本都越来越高,并且每次都返回到 xml 解析。