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

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace SortDictionary
  8. {
  9. class Program
  10. {
  11. static void Main(string[] args)
  12. {
  13. TestDictionarySort();
  14. TestDictionarySort2();
  15. Console.Read();
  16. }
  17. private static void TestDictionarySort()
  18. {
  19. SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
  20. sd.Add("", "fdsgsags");
  21. sd.Add("acb", "test test");
  22. sd.Add("", "lslgsgl");
  23. sd.Add("2bcd13", "value");
  24.  
  25. foreach (KeyValuePair<string, string> item in sd)
  26. {
  27. Console.Write("键名:" + item.Key + " 键值:" + item.Value+"\r\n");
  28. }
  29.  
  30. }
  31.  
  32. private static void TestDictionarySort2()
  33. {
  34. SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
  35. sd.Add("", "fdsgsags");
  36. sd.Add("acb", "test test");
  37. sd.Add("", "lslgsgl");
  38. sd.Add("2bcd13", "value");
  39.  
  40. Console.Write("\r\n正序排序数据:\r\n");
  41. foreach (KeyValuePair<string, string> item in sd)
  42. {
  43. Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
  44. }
  45.  
  46. //重新封装到Dictionary里(PS:因为排序后我们将不在使用排序了,所以就使用Dictionary)
  47. Dictionary<string, string> dc = new Dictionary<string, string>();
  48. foreach (KeyValuePair<string, string> item in sd.Reverse())
  49. {
  50. dc.Add(item.Key, item.Value);
  51. }
  52. sd = null;
  53. //再看其输出结果:
  54. Console.Write("\r\n反序排序数据:\r\n");
  55. foreach (KeyValuePair<string, string> item in dc)
  56. {
  57. Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
  58. }
  59. }
  60.  
  61. }
  62. }

结果:

通过字典key得到value

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

Listl转Dictionary

  1. public Dictionary<int?, string> GetForbiddenTypeList()
  2. {
  3. //var dic = new Dictionary<int?, string>();
  4. var list = new List<ForbiddenTypeDetail>();
  5. var result = BSClient.Send<ForbiddenTypeResponse>(new ForbiddenTypeRequest());
  6. if (result.DoFlag)
  7. {
  8. //foreach (var item in result.ForbiddenType)
  9. //{
  10. // if (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)
  11. // dic.Add(item.Id, item.Type);
  12. //}
  13.  
  14. list = Mapper.MappGereric<ForbiddenType, ForbiddenTypeDetail>(result.ForbiddenType).ToList();
  15. }
  16. return list.Where(item => (!string.IsNullOrEmpty(item.Type) && item.Id.HasValue)).ToDictionary(item => item.Id, item => item.Type);
  17. //return dic;
  18. }

todictionary:

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

SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关的更多相关文章

  1. SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。

    SortedDictionary<TKey, TValue> 类   表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...

  2. .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>

    无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...

  3. C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序

    使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary, 但SortedDictionary并不是那么理想,其默 ...

  4. 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1

    找一个四位数,要求该四位数的四倍刚好是该四位数的反序. 即b1b2b3b4 * 4 = b4b3b2b1 解: 第一步,确认最末位 假设 b1b2b3b4 + b4b3b2b1 = [x0]x1x2x ...

  5. Django的列表反序

    Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...

  6. leetcode:Reverse Integer(一个整数反序输出)

    Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...

  7. pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。

    http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...

  8. 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)

    目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...

  9. 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。

    namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...

随机推荐

  1. 关于jQuery中的 offset() 和 position() 的用法

    ---恢复内容开始--- 在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引入这个方法呢?这两个方法之间有 ...

  2. Visual Studio IDE 背景色该为保护眼睛色

    将背景颜色改成你想要的背景颜色. 将色调改为:85.饱和度:123.亮度:205->添加到自定义颜色->在自定义颜色选定点确定   就搞定了!

  3. 小米手机不能直接运行Android Studio程序

    小米手机不能直接运行Android Studio程序 转载自:http://www.jianshu.com/p/6588c69b42cf Problem description: Android St ...

  4. .NET:命令行解析器介绍

    背景 经常需要开发一下小工具,之前都是自己解析命令行参数,接触过动态语言社区以后,发现命令行解析有特定的模式和框架可以利用,本文介绍一个 .NET 平台的类库. 示例 需求 拷贝文件,如:CopyFi ...

  5. java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)

    说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...

  6. (转)SqlServer里DateTime转字符串

    原文:http://www.cnblogs.com/kimbosung/p/4515670.html ), )::: ), ): :::953PM ), ): ), ): ), ): ), ): :: ...

  7. 8.volatile原子性

    原子性     1.一个操作是不可中断的,即使多个线程在一起执行的时候,一旦操作执行开始,就不会被其他的线程干扰执行并导致执行中断.     2.对于静态变量int ,2个线程同时对它进行修改,线程a ...

  8. 使用 Kafka 和 Spark Streaming 构建实时数据处理系统(转)

    原文链接:http://www.ibm.com/developerworks/cn/opensource/os-cn-spark-practice2/index.html?ca=drs-&ut ...

  9. 【版本】API NDK 系统 分辨率 统计

    Android版本号 版本  API/NDK版本号  代号                        发布时间 7.1.1          25            Nougat      7 ...

  10. sed 命令编辑文本

    1.sed 概述 sed 是一个非交互式文本编辑器.它能够对文本文件和标准输入进行编辑,标准输入能够是来自键盘输入.文件重定向.字符串.变量.甚至来自于管道文本. 2.sed工作流程简述 sed在处理 ...