【发布时间】:2016-10-26 03:06:08
【问题描述】:
我在 C# 中有一个哈希表。
Hashtable people = new Hashtable();
people.Add(0, new Person("Jimmi", "Hendrix"));
people.Add(1, new Person("Bob", "Dylan"));
people.Add(2, new Person("Jim", "Morrison"));
我如何使用 Linq 按人的姓氏对这个哈希进行排序?
class Person
{
public Person(string firstName, string lastName): this(firstName, lastName, DateTime.Now)
{}
public Person(string firstName, string lastName, DateTime birthDate)
{
FirstName = firstName;
LastName = lastName;
DateOfBirth = birthDate;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public DateTime DateOfBirth { get; set; }
}
【问题讨论】:
-
所有基于 has-table 的类型(Dictionary、HashSet、Hashtable)都没有排序的概念。如果您阐明了您真正想要实现的目标,那么有人可能会建议更合适的类型或方法来实现这一目标。
-
请注意,对于在受支持的 .Net 框架(而不是 1.x)上运行的代码,使用
Dictionary<int, Person>会是更好的选择..