如何排序类的列表集合在他们的属性? [英] How to sort a List collection of classes with properties in them?

查看:174
本文介绍了如何排序类的列表集合在他们的属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的班级我的收藏列表像

I have a List of classes in my collection like

List<MyClass> test = new List<MyClass>();

在我的班级我都只是一些性质

In my classes I have just some properties

public string id {get; set;}
public DateTime date {get; set;}

现在我所得到的一些疑问,从2个不同的数据库表进行这些类。然后,我把那些2的结果从数据库表和使用foreach循环我提出一个新的MyClass的对象,并把它贴在我的测试集合。

Now I make these classes by getting some queries from 2 different database tables. I then take those 2 results from the database tables and using a foreach loop I make a new MyClass object and stick it in my "test" collection.

现在一旦所有这些类放在列表集合。我希望通过他们进行排序,并通过日期属性责令类。

Now once all these classes are put in the the list collection. I want to sort through them and order the classes by the "date" property.

我怎么能这样做?我不能为了那么权当我从数据库中获取它们,因为我是从2个不同的数据库表让他们和他们分开订购只会让下令在每个部分,但表中的一个可能有12/12/2009,所以可能表两种。因此,他们需要一起订购。

How could I do this? I can't order then right when I get them from the database since I am getting them from 2 different database tables and ordering them separately would only make it ordered in for each section but table one might have 12/12/2009 and so might table two. So they need to be ordered together.

所以,我可以一些如何使用LINQ什么命令他们?

So can I some how use linq or something to order them?

感谢

推荐答案

怎么样:

list.Sort((x,y) => DateTime.Compare(x.date, y.date));

这种种现有列表,或者:

which sorts the existing list, or:

var sorted = list.OrderBy(x=>x.date).ToList();

这将创建一个第二个列表。

which creates a second list.

这篇关于如何排序类的列表集合在他们的属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆