SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关

SortedDictionary<TKey,TValue>能对字典排序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SortDictionary
{
    class Program
    {
        static void Main(string[] args)
        {
            TestDictionarySort();
            TestDictionarySort2();
            Console.Read();
        }
        private static void TestDictionarySort()
        {
            SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
            sd.Add("321", "fdsgsags");
            sd.Add("acb", "test test");
            sd.Add("1123", "lslgsgl");
            sd.Add("2bcd13", "value");

            foreach (KeyValuePair<string, string> item in sd)
            {
                Console.Write("键名:" + item.Key + " 键值:" + item.Value+"
");
            }

        }

        private static void TestDictionarySort2()
        {
            SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
            sd.Add("321", "fdsgsags");
            sd.Add("acb", "test test");
            sd.Add("1123", "lslgsgl");
            sd.Add("2bcd13", "value");

            Console.Write("
正序排序数据:
");
            foreach (KeyValuePair<string, string> item in sd)
            {
                Console.Write("键名:" + item.Key + " 键值:" + item.Value + "
");
            }

            //重新封装到Dictionary里(PS:因为排序后我们将不在使用排序了,所以就使用Dictionary)
            Dictionary<string, string> dc = new Dictionary<string, string>();
            foreach (KeyValuePair<string, string> item in sd.Reverse())
            {
                dc.Add(item.Key, item.Value);
            }
            sd = null;
            //再看其输出结果:
            Console.Write("
反序排序数据:
");
            foreach (KeyValuePair<string, string> item in dc)
            {
                Console.Write("键名:" + item.Key + " 键值:" + item.Value + "
");
            } 
        }

    }
}

结果:

通过字典key得到value

var keywordDic = new Dictionary<int, string>()
{
{0,"搜索关键字"},
{1,"分类id"},
{2,"品牌id"}
};
var keywordCode = keywordDic[(int)item.KeyWordType];

Listl转Dictionary

        public Dictionary<int?, string> GetForbiddenTypeList()
        {
            //var dic = new Dictionary<int?, string>();
            var list = new List<ForbiddenTypeDetail>();
            var result = BSClient.Send<ForbiddenTypeResponse>(new ForbiddenTypeRequest());
            if (result.DoFlag)
            {
                //foreach (var item in result.ForbiddenType)
                //{
                //    if (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)
                //        dic.Add(item.Id, item.Type);
                //}

                list = Mapper.MappGereric<ForbiddenType, ForbiddenTypeDetail>(result.ForbiddenType).ToList();
            }
            return list.Where(item => (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)).ToDictionary(item => item.Id, item => item.Type);
            //return dic;
        }

 todictionary:

var moduleDict = adListRes.ReturnValue.AdModuleDataDto.Where(itemlist => itemlist.Data.ToList().Count > 0).ToDictionary
(itemlist => itemlist.ModuleCode, itemlist => itemlist.Data.ToList())

原文地址:https://www.cnblogs.com/shy1766IT/p/5046389.html