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("", "fdsgsags");
- sd.Add("acb", "test test");
- sd.Add("", "lslgsgl");
- sd.Add("2bcd13", "value");
- foreach (KeyValuePair<string, string> item in sd)
- {
- Console.Write("键名:" + item.Key + " 键值:" + item.Value+"\r\n");
- }
- }
- private static void TestDictionarySort2()
- {
- SortedDictionary<string, string> sd = new SortedDictionary<string, string>();
- sd.Add("", "fdsgsags");
- sd.Add("acb", "test test");
- sd.Add("", "lslgsgl");
- sd.Add("2bcd13", "value");
- Console.Write("\r\n正序排序数据:\r\n");
- foreach (KeyValuePair<string, string> item in sd)
- {
- Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
- }
- //重新封装到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("\r\n反序排序数据:\r\n");
- foreach (KeyValuePair<string, string> item in dc)
- {
- Console.Write("键名:" + item.Key + " 键值:" + item.Value + "\r\n");
- }
- }
- }
- }
结果:
通过字典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())
SortedDictionary<TKey,TValue>正序与反序排序及Dicttionary相关的更多相关文章
- SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合。
SortedDictionary<TKey, TValue> 类 表示根据键进行排序的键/值对的集合. SortedDictionary<TKey, TValue> 中的每 ...
- .net学习笔记----有序集合SortedList、SortedList<TKey,TValue>、SortedDictionary<TKey,TValue>
无论是常用的List<T>.Hashtable还是ListDictionary<TKey,TValue>,在保存值的时候都是无序的,而今天要介绍的集合类SortedList和S ...
- C# 谈Dictionary<TKey,TValue>,SortedDictionary<TKey,TValue>排序
使用过Dictionary的人都知道,当每一个Add里面的值都不会改变其顺序,所以需要需要对其排序的时候就用到SortedDictionary, 但SortedDictionary并不是那么理想,其默 ...
- 找一个四位数,要求该四位数的四倍刚好是该四位数的反序。 即b1b2b3b4 * 4 = b4b3b2b1
找一个四位数,要求该四位数的四倍刚好是该四位数的反序. 即b1b2b3b4 * 4 = b4b3b2b1 解: 第一步,确认最末位 假设 b1b2b3b4 + b4b3b2b1 = [x0]x1x2x ...
- Django的列表反序
Django虽然是python的web框架,但它不是所有的python特性都支持的. 最近在项目中遇到一个问题,需要在Django中将获得的列表反序排列,一开始我使用的是python的reverse方 ...
- leetcode:Reverse Integer(一个整数反序输出)
Question:Reverse digits of an integer. Example1: x = 123, return 321Example2: x = -123, return -321 ...
- pojg2744找一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是y的子串,或者x中的字符反序之后得到的新字符串是y的子串。
http://poj.grids.cn/practice/2744 描述现在有一些由英文字符组成的大小写敏感的字符串,你的任务是找到一个最长的字符串x,使得对于已经给出的字符串中的任意一个y,x或者是 ...
- 前端总结·基础篇·JS(二)数组深拷贝、去重以及字符串反序和数组(Array)
目录 这是<前端总结·基础篇·JS>系列的第二篇,主要总结一下JS数组的使用.技巧以及常用方法. 一.数组使用 1.1 定义数组 1.2 使用数组 1.3 类型检测 二.常用技巧 2.1 ...
- 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的顺序输出,如果传入的是一个字符串,就将字符串反序输出。
namespace test2 { class Program { /// <summary> /// 编写一个类,其中包含一个排序的方法Sort(),当传入的是一串整数,就按照从小到大的 ...
随机推荐
- 关于jQuery中的 offset() 和 position() 的用法
---恢复内容开始--- 在jQuery中有两个获取元素位置的方法offset()和position().position()方法是在1.2.6版本之后加入的,为什么要引入这个方法呢?这两个方法之间有 ...
- Visual Studio IDE 背景色该为保护眼睛色
将背景颜色改成你想要的背景颜色. 将色调改为:85.饱和度:123.亮度:205->添加到自定义颜色->在自定义颜色选定点确定 就搞定了!
- 小米手机不能直接运行Android Studio程序
小米手机不能直接运行Android Studio程序 转载自:http://www.jianshu.com/p/6588c69b42cf Problem description: Android St ...
- .NET:命令行解析器介绍
背景 经常需要开发一下小工具,之前都是自己解析命令行参数,接触过动态语言社区以后,发现命令行解析有特定的模式和框架可以利用,本文介绍一个 .NET 平台的类库. 示例 需求 拷贝文件,如:CopyFi ...
- java集合(ArrayList,Vector,LinkedList,HashSet,TreeSet的功能详解)
说起集合,我们会潜意识里想到另外一个与之相近的名词——数组,OK!两者确实有相似之处,但也正是这点才是我们应该注意的地方,下面简单列出了两者的区别(具体功能的不同学习这篇文章后就会明白了): 数组 长 ...
- (转)SqlServer里DateTime转字符串
原文:http://www.cnblogs.com/kimbosung/p/4515670.html ), )::: ), ): :::953PM ), ): ), ): ), ): ), ): :: ...
- 8.volatile原子性
原子性 1.一个操作是不可中断的,即使多个线程在一起执行的时候,一旦操作执行开始,就不会被其他的线程干扰执行并导致执行中断. 2.对于静态变量int ,2个线程同时对它进行修改,线程a ...
- 使用 Kafka 和 Spark Streaming 构建实时数据处理系统(转)
原文链接:http://www.ibm.com/developerworks/cn/opensource/os-cn-spark-practice2/index.html?ca=drs-&ut ...
- 【版本】API NDK 系统 分辨率 统计
Android版本号 版本 API/NDK版本号 代号 发布时间 7.1.1 25 Nougat 7 ...
- sed 命令编辑文本
1.sed 概述 sed 是一个非交互式文本编辑器.它能够对文本文件和标准输入进行编辑,标准输入能够是来自键盘输入.文件重定向.字符串.变量.甚至来自于管道文本. 2.sed工作流程简述 sed在处理 ...