如何排序类的列表集合在他们的属性?属性、列表、在他们的

由网友(拜你所赐)分享简介:我的班级我的收藏列表像I have a List of classes in my collection likeList test = new List();在我的班级我都只是一些性质In my classes I have just some propertiespubl...

我的班级我的收藏列表像

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.

阅读全文

相关推荐

最新文章