C# foreach循环中的Orderby datetime查询
嗨,我有一个休息服务,它有一个学生名单,每个学生都有一个日期时间附加到那里 在我的客户端,我想在这个日期之前从最新的开始订购,在我的客户端,代码如下所示:C# foreach循环中的Orderby datetime查询,c#,linq,C#,Linq,嗨,我有一个休息服务,它有一个学生名单,每个学生都有一个日期时间附加到那里 在我的客户端,我想在这个日期之前从最新的开始订购,在我的客户端,代码如下所示: public FindStudent() { InitializeComponent(); string uriGroups = "http://localhost:8000/Service/Student"; XDocument xDoc = XDocument.Load(uri
public FindStudent()
{
InitializeComponent();
string uriGroups = "http://localhost:8000/Service/Student";
XDocument xDoc = XDocument.Load(uriGroups);
它按first created列出了学生,我想我可以在foreach var节点中添加orderby查询,但我认为这行不通,还有什么我可以这样做吗?为了根据日期和时间对某些内容进行排序,您需要在for each循环中将其转换为日期和时间,您只需循环从xml文档中获取的纯文本,它不会被排序,因为它是文本,但您可以使用varaibles
Studentid,FirstName-LastName创建一个学生类“TimeAdded TimeAdded
再次循环浏览文档将每个信息写入对象学生将每个对象添加到列表按您喜欢的方式排序列表,然后我认为它应该按您想要的顺序显示 您可以使用Linq转换XML,如下所示
var temp = from feed in xDoc.Descendants("Students")
orderby Convert.ToDateTime(feed.Element("CreatDate").Value) descending
select new Student
{
ID = Convert.ToInt32(feed.Element("ID").Value),
//rest of the properties
};
这与WCF、XAML、REST或WPF有什么关系?我不能在foreach循环中使用select语句。在中间数据结构中获取Student集合(在foreach循环之前),对其进行排序,然后可以在foreach循环中使用。XElement可以转换为DateTime:
(DateTime)feed.Element(“CreatDate”)
XDocument xDoc = XDocument.Load(uriGroups);
var sortedXdoc = xDoc.Descendants("Student")
.OrderByDescending(x => Convert.ToDateTime(x.Element("TimeAdded").Value));