C# Enum.GetValues() 上的 OrderBy

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19599252/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 15:23:39  来源:igfitidea点击:

OrderBy on Enum.GetValues()

c#linqenums

提问by Cameron Tinker

I'm populating a DropDownListin MVC 4 from an enumand I want to order the enum values from largest to smallest. However, there doesn't seem to be a direct way of approaching this. Currently, I'm using this code to add to a dictionary with the key being the ID and the value being the display text:

我正在DropDownList从 an填充MVC 4 中的 aenum并且我想从最大到最小对枚举值进行排序。但是,似乎没有直接的方法来解决这个问题。目前,我正在使用此代码添加到字典中,键是 ID,值是显示文本:

var priorities = Enum.GetValues(typeof(Models.Priority)).OfType<Models.Priority>().ToList();

for (int i = priorities.Count - 1; i >= 0; i--)
{
    Models.Priority priority = priorities[i];
    prioritiesDictionary.Add((int)priority, "Priority " + ((int)priority).ToString());
}

I don't believe that putting enum values into a list and looping backwards is the most efficient method. There are only four values in the enum, but is there a better way to perform an OrderByoperation on what is returned from Enum.GetValues? I know it might be minimal performance impact doing it the way I am, but I want to know for larger enums.

我不相信将枚举值放入列表并向后循环是最有效的方法。枚举中只有四个值,但是有没有更好的方法来OrderBy对返回的内容执行操作Enum.GetValues?我知道按照我的方式这样做可能对性能的影响很小,但我想知道更大的枚举。

采纳答案by Jon Skeet

Sounds like you just want:

听起来你只想:

var priorities = ((Models.Priority[]) Enum.GetValues(typeof(Models.Priority)))
                                          .OrderByDescending(x => x);

Or to avoid quite as many brackets:

或者为了避免尽可能多的括号:

var priorities = (Models.Priority[]) Enum.GetValues(typeof(Models.Priority));
var ordered = priorities.OrderByDescending(x => x);

It's not clear how your current code is helping you, by giving you a dictionary - but the above will definitely give you a sequence of enum values, ordered from highest to lowest. You don't need to cast to int, because enum values of the same type are already comparable to each other.

目前尚不清楚您当前的代码如何通过为您提供字典来帮助您 - 但以上肯定会给您一个枚举值序列,从高到低排序。您不需要强制转换为int,因为相同类型的枚举值已经可以相互比较。

If you need a list, just call ToList()after the OrderByDescendingcall.

如果您需要清单,请ToList()在通话后OrderByDescending致电。

回答by usr

What about this:

那这个呢:

Enum.GetValues(typeof(Models.Priority))
    .Cast<Models.Priority>()
    .OrderBy(x => (int) x)
    .ToList();

Don't use OfTypebecause that silently throws away non-matching items. It is almost always a mistake.

不要使用,OfType因为它会默默地丢弃不匹配的项目。这几乎总是一个错误。

回答by LawMan

Based on the above, I create a reusable function...

基于以上,我创建了一个可重用的函数......

    public static IOrderedEnumerable<TEnum> Sort<TEnum>()
    {
        return ((TEnum[]) Enum.GetValues(typeof (TEnum))).OrderBy(x => x.ToString());
    }