.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数组形式。


本文转自TBHacker博客园博客,原文链接:http://www.cnblogs.com/jiqing9006/p/6958282.html,如需转载请自行联系原作者

相关文章
|
21天前
.net-去重的几种情况
.net-去重的几种情况
28 0
.net中Dategrid一些基本操作
经过一段时间项目的开发,对于DataGrid控件的运用有了深刻理解,在做项目的过程中,对于概念、属性、基本运用、操作进行了总结,在这里小编与大家分享我的总结。
一起谈.NET技术,.NET 3.x新特性之自动属性及集合初始化
  今天公司弄了个VS2008 beta 2中文版,虽然很大一部分是为了JS的智能提示外,也应该好好的温习一下.NET的一些新特性,由于以前写过一些文章,但是都没有系统的学过,都只是尝一下新鲜感。不知道从那开始,所以今天就来看看自动话属性,以及对象初始化和集合初始化的一些新特性。
642 0
|
Web App开发 JavaScript .NET
一起谈.NET技术,VS 2010 和 .NET 4.0 系列之《添加引用对话框的改进》篇
本系列文章导航 VS 2010 和 .NET 4.0 系列之《ASP.NET 4 中的SEO改进 》篇 VS 2010 和 .NET 4.0 系列之《干净的Web.Config文件 》篇 VS 2010 和 .
915 0
|
XML JSON JavaScript
.NET json格式操作(转载)
//需要先下载Newtonsoft.Json组件 JSON(全称为JavaScript Object Notation) 是一种轻量级的数据交换格式。它是基于JavaScript语法标准的一个子集。
899 0

热门文章

最新文章