如何检查字符串值中的枚举? [英] how to check if string value is in the Enum list?

查看:107
本文介绍了如何检查字符串值中的枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的查询字符串,我有一个年龄的变量?年龄= New_Born

In my querystring, I have an age variable "?age=New_Born"

有没有办法,我可以检查此字符串值New_Born是我的枚举列表

is there a way I can check if this string value "New_Born" is in my Enum list

[Flags]
public enum Age
{
    New_Born = 1,
    Toddler = 2,
    Preschool = 4,
    Kindergarten = 8
}

我可以使用if语句的权利,但如果我的枚举列表变大了,我想找到一个更好的方式来做到这一点。我正在考虑使用LINQ,只是不知道如何做到这一点。

I could use if statement for right now, but if my Enum list gets bigger, I want to find a better way to do it. I am thinking about to use linq, just not sure how to do it.

推荐答案

您可以使用:

 Enum.IsDefined(typeof(Age), youragevariable)

这篇关于如何检查字符串值中的枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆