• .Net中字典的使用


            /// <summary>
            /// 获取用户市信息
            /// </summary>
            /// <param name="CustomerId"></param>
            /// <returns></returns>
            [HttpPost]
            public ActionResult GetCustomerProvinceAndCity(int CustomerId)
            {
                // 检测是否已有记录
                IList<GenericAttribute> customerAttributes = _genericAttributeService.GetAttributesForEntity(CustomerId, "Customer");
                Dictionary<string, string> myDictionary = new Dictionary<string, string>();
                foreach (GenericAttribute item in customerAttributes)
                {
                    //if (item.Key == "ProvinceName")
                    //{
                    //    myDictionary["ProvinceName"] = item.Value;
                    //    continue;
                    //}
    
                    //if (item.Key == "CityName")
                    //{
                    //    myDictionary["CityName"] = item.Value;
                    //    continue;
                    //}
    
                    if (item.Key == "ProvinceName")
                    {
                        myDictionary.Add("ProvinceName", item.Value);
                        continue;
                    }
    
                    if (item.Key == "CityName")
                    {
                        myDictionary.Add("CityName", item.Value);
                        continue;
                    }
                }
    
                if (myDictionary.Count > 0)
                { // 存在
                    return Json(new { result = true, info = myDictionary.ToArray(), msg = "获取成功" });
                }
                else
                { // 不存在
                    return Json(new { result = true, info = myDictionary.ToArray(), msg = "不存在" });
                }
            }
    
    

    Dictionary 方便存储 key/value这种类型的数据,这个比Array要灵活一些。
    上面的两种方式都可以保存数据。

    判断数据数量,是通过Count属性。

    最后返回数据的时候,可以统一转化为Array数组形式。

  • 相关阅读:
    zabbix 3.4自动化监控(6)
    zabbix 3.4 web监控(5)
    童年
    CSDN中快速转载别人文章
    10个提升效率的编程好习惯(转载)
    解决vscode中vim插件,不能使用Ctrl+c
    英语复习安排
    比勤奋更能拉开人生差距的,是成长型思维
    转载文章
    红宝书考研资料
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6958282.html
Copyright © 2020-2023  润新知