【发布时间】:2013-09-05 14:20:41
【问题描述】:
这里我有一个方法,它将返回匹配的参数,只返回字符串作为返回类型,并且它工作正常......
private static string GetSortedParameter(string modelValue)
{
string returnValue = null;
if (modelValue == "UserId")
{
returnValue = "UsrID";
}
if (modelValue == "Status")
{
returnValue = "TransactionStatusTypeName";
}
if (modelValue == "ProjectCaseNumber")
{
returnValue = "PROJCASE";
}
if (modelValue == "CP")
{
returnValue = "CPNumber";
}
if (modelValue == "ItemID")
{
returnValue = "ItemID";
}
if (modelValue == "TypeOfChange")
{
returnValue = "TransactionTypeName";
}
if (modelValue == "ChangeDescription")
{
returnValue = "TransactionTypeDescription";
}
if (modelValue == "CreatedOnEnd")
{
returnValue = "CreatedDateTime";
}
if (modelValue == "UpdatedOnEnd")
{
returnValue = "UpdatedDateTime";
}
if (modelValue == "Comment")
{
returnValue = "Comments";
}
return returnValue;
}
我在这里调用这个方法
if (request.Sorts != null && request.Sorts.Count > 0)
{
sortField = request.Sorts[0].Member;
sortDirection = request.Sorts[0].SortDirection.ToString();
}
string SortFieldParameter = GetSortedParameter(sortField);
但是我想对这种类型使用枚举,我如何将枚举用于这种类型的匹配参数,它将一个值作为输入参数并给出匹配的值.....
请您对此提出任何想法和解决方案....
我也在为此寻找任何通用解决方案.....
【问题讨论】:
-
拥有
Dictionary<string, string>并快速查找不是更容易吗? -
我想你想要一个
switch声明。 -
if statement使用不当使用switchofif-else if chain -
Patryk Ćwiek 是最好的解决方案,即使您设置了枚举和
Dictionary<MyEnum, string>。