我有两个枚举
public enum YogabandEventDuration
{
[Display(Name = "30 min")]
ThirtyMinutes = 30,
[Display(Name = "45 min")]
FortyFiveMinutes = 45,
[Display(Name = "60 min")]
SixtyMinutes = 60,
[Display(Name = "75 min")]
SeventyFiveMinutes = 75,
[Display(Name = "90 min")]
NinetyMinutes = 90
}
public enum PrivatePrice
{
[Display(Name = "$8.95")]
ThirtyMinute = 895,
[Display(Name = "$14.95")]
FortyFiveMinute = 1495,
[Display(Name = "$19.95")]
SixtyMinute = 1495,
[Display(Name = "$24.95")]
SeventyFiveMinute = 2495,
[Display(Name = "$27.95")]
NinetyMinute = 2795
}
有没有办法将每个枚举中的值一对一匹配,以便我可以像这样进行搜索
var theClassPrice = GetPrice(YogabandEventDuration);
传递ThirtyMinutes将返回895
答案 0 :(得分:2)
按照@SteveJ的建议,我个人会采用类似的方法
var privateSession = new Dictionary<Session, double>()
{
{ Session.ThirtyMinute, 8.95},
{ Session.FortyFiveMinute, 14.95 },
{ Session.SixtyMinute, 19.95 },
{ Session.SeventyFiveMinute, 24.95 },
{ Session.NinetyMinute, 27.95 }
};
然后使用一个枚举作为会话时间
public enum Session
{
ThirtyMinute,
FortyFiveMinute,
SixtyMinute,
SeventyFiveMinute,
NinetyMinute
}
答案 1 :(得分:1)
即使这确实很奇怪: 您可以parse一个字符串来获取等于枚举的值。
var time = YogabandEventDuration.ThrityMinutes.ToString();
var price = Enum.Parse(typeof(PrivatePrice), time );
var intValue = (int)price;
为您提供一个简单的功能:
private int GetPrice(YogabandEventDuration duration) {
return (int)Enum.Parse(typeof(PrivatePrice), duration.ToString());
}