[英]How to perform LINQ query over Enum?
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. 我需要将它绑定到一个
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 Enum
list to a Combobox
. 但是,我觉得代码有点乱,并不是将过滤后的
Enum
列表绑定到Combobox
的正确方法。 Can anyone suggest a more robust way of doing this? 任何人都可以建议一个更强大的方法吗?
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个状态。
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
: 如果您希望能够更改项目列表,只需将它们添加到
List<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));
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. 你也可以处理
List<T>
并返回数组本身。 As long as you know these are the items you want, then there's no need for Linq. 只要您知道这些是您想要的项目,那么Linq就没有必要了。
Steps: 脚步:
enum
values and cast the results to the type of the enum
enum
值并将结果转换为enum
的类型 enum
values by their integer values (otherwise they sort naturally by unsigned magnitude) enum
值进行排序(否则它们按无符号大小自然排序) Code: 码:
return Enum.GetValues(typeof(Activity.StatusEnum))
.Cast<Activity.StatusEnum>()
.OrderBy(se =>(int)se)
.Take(4);
Output: 输出:
Open Rejected Accepted Started
Open Rejected Accepted Started
First, if possible, I'd make your enum values powers of 2, so they could be OR'd together. 首先,如果可能的话,我会使你的枚举值为2的幂,所以它们可以一起进行OR运算。
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
};
}
". . . only show the first 4 statuses and ignore the rest."
“......只显示前4种状态而忽略其余部分。”
To get the first n
elements of an IEnumerable<T>
, use the Take
method: 要获取
IEnumerable<T>
的前n
元素,请使用Take
方法:
return Enum.GetValues(typeof(Activity.StatusEnum))
.Cast<Activity.StatusEnum>()
.Take(4)
.ToList();
怎么样的东西:
.Where(x => x <= Activity.StatusEnum.Started)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.