如何将字符串转换为使用C#在Linq中枚举?将字符串转换为在Linq中枚举为SQL
下面是否也该型铸造的LINQ工作?:
(Audience)Enum.Parse(typeof(Audience), value, true);
如果有,请告诉我,我该如何使用呢?
如何将字符串转换为使用C#在Linq中枚举?将字符串转换为在Linq中枚举为SQL
下面是否也该型铸造的LINQ工作?:
(Audience)Enum.Parse(typeof(Audience), value, true);
如果有,请告诉我,我该如何使用呢?
鉴于枚举
enum Foo{A, B, C}
下面的代码进行转换,从enum
到string
,反之亦然:
var values =
from name in Enum.GetNames(typeof(Foo))
select (Foo)Enum.Parse(typeof(Foo), name, true);
所以,是铸造厂。但是,请记住,如果Enum.Parse
方法收到无法解析的值,上面的查询将抛出ArgumentException
。
此更新版本只返回解析成功地
enum Foo{A, B, C}
var values =
from name in Enum.GetNames(typeof(Foo))
where Enum.IsDefined(typeof(Foo), name)
select (Foo)Enum.Parse(typeof(Foo), name, true);
你试过值? – Botz3000
http://stackoverflow.com/questions/1426577/how-to-use-flags-enums-in-linq-to-entities-queries – Habib