37

下面是我的Enumerator List

public enum StatusEnum
{
    Open = 1,
    Rejected = 2,
    Accepted = 3,
    Started = 4,
    Completed = 5,
    Cancelled = 6,
    Assigned = 7
}

我需要将它绑定到一个Combobox,但是,只显示一些特定的状态并忽略其余的。

这是我到目前为止所拥有的:

public static List<Activity.StatusEnum> StatusList()
{
        IEnumerable<Activity.StatusEnum> query = Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>()
                        .Where(x => x == Activity.StatusEnum.Open
                            || x == Activity.StatusEnum.Rejected
                            || x == Activity.StatusEnum.Accepted
                            || x == Activity.StatusEnum.Started);
        return query.ToList();
}

但是,我觉得代码有点乱,不是将过滤Enum列表绑定到Combobox. 任何人都可以提出一种更强大的方法吗?

更新

我可能需要更改选择顺序。所以我需要一个通用的解决方案,它不仅可以获得前 X 个状态。

4

6 回答 6

49
return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where((n, x) => x < 4);

如果您希望能够更改项目列表,只需将它们添加到 aList<Activity.StatusEnum>并使用Contains

var listValid = new List<Activity.StatusEnum>() { Activity.StatusEnum.Open, Activity.StatusEnum.Rejected, Activity.StatusEnum.Accepted, Activity.StatusEnum.Started };
return Enum.GetValues(typeof(Activity.StatusEnum)).Cast<Activity.StatusEnum>().Where(n => listValid.Contains(n));
于 2013-06-13T16:11:36.843 回答
12

好吧,如果您要硬编码应该在列表中的项目,为什么不这样做:

public static List<Activity.StatusEnum> StatusList()
{
    return new List<Activity.StatusEnum>
    { 
        Activity.StatusEnum.Open, 
        Activity.StatusEnum.Rejected, 
        Activity.StatusEnum.Accepted, 
        Activity.StatusEnum.Started 
    };
}

您也可以处理List<T>并返回数组本身。只要您知道这些是您想要的项目,那么就不需要 Linq。

于 2013-06-13T16:13:08.130 回答
11

Steps:

  • Get the enum values and cast the results to the type of the enum
  • Sort the enum values by their integer values (otherwise they sort naturally by unsigned magnitude)
  • Take the first 4

Code:

return Enum.GetValues(typeof(Activity.StatusEnum))
.Cast<Activity.StatusEnum>()
.OrderBy(se =>(int)se)
.Take(4);

Output:

Open Rejected Accepted Started

于 2013-06-13T16:13:51.947 回答
8

First, if possible, I'd make your enum values powers of 2, so they could be OR'd together.

public enum StatusEnum
{
    Open = 1,
    Rejected = 2,
    Accepted = 4,
    Started = 8,
    Completed = 16,
    Cancelled = 32,
    Assigned = 64
}

Then you could do something like this:

public static List<Activity.StatusEnum> StatusList()
{
    var statusesToShow = Activity.StatusEnum.Open | Activity.StatusEnum.Rejected | Activity.StatusEnum.Accepted | Activity.StatusEnum.Started;

    return Enum
        .GetValues(typeof(Activity.StatusEnum))
        .Cast<Activity.StatusEnum>()
        .Where(x => (x & statusesToShow) == x)
        .ToList();
}

EDIT: In light of the fact that you can't change the enum values, I'd just recommend you use something like:

public static List<Activity.StatusEnum> StatusList()
{
    return new List<Activity.StatusEnum> {
        Activity.StatusEnum.Open, 
        Activity.StatusEnum.Rejected, 
        Activity.StatusEnum.Accepted, 
        Activity.StatusEnum.Started
    };
}
于 2013-06-13T16:14:40.760 回答
6

“.. . . 只显示前 4 个状态,其余的忽略。”

要获取nan 的第一个元素IEnumerable<T>,请使用以下Take方法:

return Enum.GetValues(typeof(Activity.StatusEnum))
    .Cast<Activity.StatusEnum>()
    .Take(4)
    .ToList();
于 2013-06-13T16:13:05.400 回答
3

类似的东西怎么样:

.Where(x => x <= Activity.StatusEnum.Started)
于 2013-06-13T16:12:01.283 回答