数组和键/值对的用法(文字转换相对应数字和文字)代替switch

1      string[] MethodRepayment = { "按月付息到期还本", "到期一次性还本息", "等额本息还款方式", "等额本金还款方式" };
2         KeyValuePair<string, string>[] pair = {
3                     new KeyValuePair<string,string>("等额本息还款方式","1"),
4                     new KeyValuePair<string,string>("按月付息到期还本","2"),
5                     new KeyValuePair<string,string>("到期一次性还本息","3"),
6                     new KeyValuePair<string,string>("到期一次性还本息","5")
7                                             };

以上是定义数组和键值对。

var result = pair.FirstOrDefault(b => b.Key == MethodRepayment[Convert.ToInt32(row["MethodRepayment"])]);//获取相对应的键值对
然后用result.Value这个方法就可以获取相对应的值
原文地址:https://www.cnblogs.com/zheng510ke/p/4538177.html