Dictionary、List<KeyValuePair<i>>的组合使用

来源:互联网 发布:沪昆高铁质量 知乎 编辑:程序博客网 时间:2024/05/21 18:49
菜菜遇到一个有意思的问题:Dictionary字典没有排序功能,遇到需要特殊排序的还要倒腾成List<KeyValuePair<>>,再在sort()函数中写委托排序;而List<KeyValuePair<>>只能支持读取操作,即只能get读取操作,无法set改变值;所以遇到既需要遍历改变某些值,然后再按照所需排序这样的操作,往往需要Dictionary和List<KeyValuePair<>>一起操作完成,如下所示:


 private List<KeyValuePair<int,string>> getdic(List<KeyValuePair<int, string>> list3, List<string> list5,List<string> list6)
        {
            Dictionary<string, int> dic = new Dictionary<string, int>();


            List<KeyValuePair<int, string>> list = new List<KeyValuePair<int, string>>();


            foreach (KeyValuePair<int, string> var in list3)
            {
                dic.Add(var.Value,2);


            }


            //处理既往病史
            foreach (string key in list5)
            {
                switch (key)
                {
                    case "高血压":
                    case "糖尿病":
                    case "高血脂":
                    case "肿瘤":
                    case "脑卒中":
                        {
                            if (dic.ContainsKey(key))
                            {
                               dic[key]=dic[key]+1;  微笑字典中值可以改变,但是如果是KeyValuePair,其key与value属性都是get类型,所以只能读取,但是不能设定值
                            }
                            else
                            {
                                dic.Add(key, 3);
                            }


                        }
                        break;


                    case "冠心病/心绞痛":
                        {


                            if (dic.ContainsKey("心"))
                            {
                                dic["心"] = dic["心"] + 1;
                            }
                            else
                            {
                                dic.Add("心", 3);
                            }




                        }
                        break;
                    case "慢性支气管炎":
                        {
                            if (dic.ContainsKey("肺"))
                            {
                                dic["肺"] = dic["肺"] + 1;
                            }
                            else
                            {
                                dic.Add("肺", 3);
                            }
                        }
                        break;
                    case "肝炎":
                        {
                            if (dic.ContainsKey("肝"))
                            {
                                dic["肝"] = dic["肝"] + 1;
                            }
                            else
                            {
                                dic.Add("肝", 3);
                            }
                        }
                        break;


                    case "慢性胃炎/胃溃疡":
                        {
                            if (dic.ContainsKey("胃"))
                            {
                                dic["胃"] = dic["胃"] + 1;
                            }
                            else
                            {
                                dic.Add("胃", 3);
                            }
                        }
                        break;
                    case "慢性肠炎":
                        {
                            if (dic.ContainsKey("小肠"))
                            {
                                dic["小肠"] = dic["小肠"] + 1;
                            }
                            else
                            {
                                dic.Add("小肠", 3);
                            }
                        }
                        break;
                    default:
                        break;
                }
            }


            //处理疾病家族史
            foreach (string key in list6)
            {
                switch (key)
                {
                    case "高血压":
                    case "中风/脑溢血":
                    case "糖尿病":
                    case "肿瘤":
                        {
                            if (dic.ContainsKey(key))
                            {
                                dic[key] = dic[key] + 1;
                            }
                            else
                            {
                                dic.Add(key, 3);
                            }
                        }
                        break;
                    case "冠心病/心绞痛":
                        {
                            if (dic.ContainsKey("心"))
                            {
                                dic["心"] = dic["心"] + 1;
                            }
                            else
                            {
                                dic.Add("心", 3);
                            }


                        }
                        break;


                    default:
                        break;
                }
            }


            //return dic;
            foreach (string key in dic.Keys)
            {
                list.Add(new KeyValuePair<int, string>(dic[key], key));微笑把字典转化成List<KeyValuePair<>>在sort()函数中写委托按照自己想要的方式排序
            }


            list.Sort(delegate(KeyValuePair<int, string> s1, KeyValuePair<int, string> s2)
            {
                return s2.Key.CompareTo(s1.Key);
            
            });

            return list;


        }
0 0
原创粉丝点击