SortedDictionary构建泛型排序字典
SortedDictionary<int, UserInfo> users = new SortedDictionary<int, UserInfo>();
//为泛型排序字典添加3个元素,注意键是按照3、2、1的顺序添加
users.Add(3, new UserInfo(1, "User01", "01"));
users.Add(2, new UserInfo(2, "User02", "02"));
users.Add(1, new UserInfo(3, "User03", "03"));
//由于泛型排序字典的元素自动按照键进行排序,所以元素按照键由小到大的顺序输出
Response.Write("LINQ排序前的查询结果<br/>");
foreach (var item in users)
{
Response.Write(string.Format("({0},{1})", item.Key, item.Value.UserName));
Response.Write("<br/>");
}
//使用LINQ对泛型排序字典进行排序操作
var query = from item in users
orderby item.Value.UserName
select item;
Response.Write("LINQ排序后的查询结果<br/>");
foreach (var item in query)
{
Response.Write(string.Format("({0},{1})", item.Key, item.Value.UserName));
Response.Write("<br/>");
}