C# 如何通过 Enum 执行 LINQ 查询?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17091917/
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 08:33:13  来源:igfitidea点击:

How to perform LINQ query over Enum?

c#.netlinqdata-bindingenums

提问by Learner

Below is my Enumerator List:

以下是我的Enumerator List

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

I need to bind this to a Combobox, but, only show a few specific statuses and ignore the rest.

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

This is what I have so far:

这是我到目前为止:

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();
}

However, I feel that the code is little messy and is not a correct approach to bind filtered Enumlist to a Combobox. Can anyone suggest a more robust way of doing this?

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

Update

更新

I might need to change the Order of selection. So I need a generic solution which doesn't only get the first X number of statuses.

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

采纳答案by p.s.w.g

Well if you're going to hard code the items that should be in the list anyway, why not just do this:

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

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

You could also dispose of the List<T>and just return the array itself. As long as you know these are the items you want, then there's no need for Linq.

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

回答by It'sNotALie.

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

If you want to be able to change the list of items, just add them into a List<Activity.StatusEnum>and use Contains:

如果您希望能够更改项目列表,只需将它们添加到 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));

回答by John Bustos

How about something along the lines of:

怎么样?

.Where(x => x <= Activity.StatusEnum.Started)

回答by Timothy Shields

". . . only show the first 4 statuses and ignore the rest."

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

To get the first nelements of an IEnumerable<T>, use the Takemethod:

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

return Enum.GetValues(typeof(Activity.StatusEnum))
    .Cast<Activity.StatusEnum>()
    .Take(4)
    .ToList();

回答by Stephen Kennedy

Steps:

脚步:

  • Get the enumvalues and cast the results to the type of the enum
  • Sort the enumvalues by their integer values (otherwise they sort naturally by unsigned magnitude)
  • Take the first 4
  • 获取enum值并将结果转换为enum
  • enum整数值对值进行排序(否则按无符号大小自然排序)
  • 拿第一个 4

Code:

代码:

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

Output:

输出:

Open Rejected Accepted Started

打开 拒绝 接受 开始

回答by Chris Doggett

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

首先,如果可能的话,我会让你的 enum 值是 2 的幂,这样它们就可以被 OR'd 在一起。

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
    };
}