【发布时间】:2016-05-12 16:45:13
【问题描述】:
大家好,我一直在阅读许多与我想做的问题类似的帖子,但不幸的是,其中任何一个都与我的问题相符。实际上我有用于填充下拉列表的字典,因为字典中的值无法按字母顺序组织(不一致,您将在下面的代码中看到值)我如何例如创建数组或列表使用序列号,按此 ID 对值进行排序,然后将组织后的值放入新字典中......或任何其他方式。感谢您的快速帮助。
这实际上是我的代码:
public static Dictionary<string, string> GetDocumentTypes()
{
Dictionary<string, string> docTypeList = new Dictionary<string, string>();
//Values inserted manually for the moment
docTypeList.Add(Guid.NewGuid().ToString(), "F17- MyValue17");
docTypeList.Add(Guid.NewGuid().ToString(), "F19- MyValue19");
docTypeList.Add(Guid.NewGuid().ToString(), "F20- MyValue20");
docTypeList.Add(Guid.NewGuid().ToString(), "F21- MyValue21");
docTypeList.Add(Guid.NewGuid().ToString(), "F2- MyValue2");
docTypeList.Add(Guid.NewGuid().ToString(), "F1- MyValue1");
docTypeList.Add(Guid.NewGuid().ToString(), "F3- MyValue3");
docTypeList.Add(Guid.NewGuid().ToString(), "F4- MyValue4");
return docTypeList;
}
//这里我尝试用这些值“有序”填充下拉列表
字典 doctypesource = GetDocumentTypes();
var ordered = doctypesource.OrderBy(x => x.Value);
DocumentType.DataSource = ordered;
DocumentType.DataValueField = "Key";
DocumentType.DataTextField = "Value";
DocumentType.DataBind();
DocumentType.Items.Insert(0, new ListItem());
最后,当填充下拉列表时,值没有排序,但它们得到的顺序如下:F1- MyValue1, F17- MyValue17, F19- MyValue19, F2- MyValue2, F20- MyValue20, F21- MyValue21, F3- MyValue3 , F4- MyValue4;
所以我想要这样的顺序:F1- MyValue1, F2- MyValue2, F3- MyValue3, F4- MyValue4, F17- MyValue17, F19- MyValue19, F20- MyValue20, F21- MyValue21
【问题讨论】:
标签: c# asp.net arrays list dictionary