如何在C#中对ResourceSet进行排序

3
我有一个名为filetypes.resx的资源文件,我已经想出了如何将资源值绑定到下拉列表,但我不知道如何对ResourceSet的值进行排序。
到目前为止,我所做的是: FileTypes.resx
  • 名称,值
  • A,1

  • B,2

  • C,3

绑定下拉列表的代码

DropDownList1.DataSource = Resources.FileTypes.ResourceManager.GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true);
DropDownList1.DataTextField = "Key";
DropDownList1.DataValueField = "Value";
DropDownList1.DataBind();

结果

 A
 C
 B
As you can see the result is not sorted. Please help me to solve this issue.

非常感谢您的提前帮助:)
3个回答

8
资源集合有一个IDictionaryEnumerator,所以我猜它的项是DictionaryEntry类型的,请尝试按照以下方式对数据源进行排序:
DropDownList1.DataSource = Resources.FileTypes.ResourceManager
    .GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true)
    .OfType<DictionaryEntry>()
    .OrderBy(i => i.Value);

哎呀,我没有打开Visual Studio,所以如果它不能立即工作,请不要责怪我 :)


1

ResourceSet有一个名为"Table"的属性,类型为HashTable。也许你可以通过手动或使用LINQ从表中生成已排序的列表,然后将此列表分配给DropDownList1.DataSource


0

Hashtable tbl = Resources.FileTypes.ResourceManager.
GetResourceSet(System.Globalization.CultureInfo.CurrentCulture, true, true).Table;
List source = new List(tbl.Values);
source.Sort();
DropDownList1.DataSource = source;
DropDownList1.DataBind();


虽然这段代码可能回答了问题,但提供关于为什么和/或如何回答问题的额外上下文会显著提高其长期价值。请编辑您的答案以添加一些解释。 - gevorg

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接