比较枚举值的代码中最好的方法是什么?例如,如果我有以下枚举类型:
public enum Level : short {
Low,
FairlyLow,
QuiteLow,
NotReallyLow,
GettingHigh,
PrettyHigh,
High,
VeryHigh,
}
我希望能够写出如下语句:
from v in values select v where v > Level.QuiteLow
答案 0 :(得分:4)
您需要将枚举值转换为其数值,因为枚举值不具有可比性:
from v in values where (short)v > (short)Level.QuiteLow select v
编辑:实际上这不是真的:枚举值 可比较,所以这段代码工作正常:
from v in values where v > Level.QuiteLow select v