博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Dictionary及KeyValuePair使用
阅读量:5265 次
发布时间:2019-06-14

本文共 3584 字,大约阅读时间需要 11 分钟。

///         /// 除去数组中的空值和签名参数并以字母a到z的顺序排序        ///         /// 过滤前的参数组        /// 
过滤后的参数组
public static Dictionary
FilterPara(SortedDictionary
dicArrayPre) { Dictionary
dicArray = new Dictionary
(); foreach (KeyValuePair
temp in dicArrayPre) { if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null) { dicArray.Add(temp.Key, temp.Value); } } return dicArray; }
///         /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串        ///         /// 需要拼接的数组        /// 
拼接完成以后的字符串
public static string CreateLinkString(Dictionary
dicArray) { StringBuilder prestr = new StringBuilder(); foreach (KeyValuePair
temp in dicArray) { prestr.Append(temp.Key + "=" + temp.Value + "&"); } //去掉最後一個&字符 int nLen = prestr.Length; prestr.Remove(nLen - 1, 1); return prestr.ToString(); }
///         /// 把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode        ///         /// 需要拼接的数组        /// 字符编码        /// 
拼接完成以后的字符串
public static string CreateLinkStringUrlencode(Dictionary
dicArray, Encoding code) { StringBuilder prestr = new StringBuilder(); foreach (KeyValuePair
temp in dicArray) { prestr.Append(temp.Key + "=" + HttpUtility.UrlEncode(temp.Value, code) + "&"); } //去掉最後一個&字符 int nLen = prestr.Length; prestr.Remove(nLen - 1, 1); return prestr.ToString(); }

 

C# get keys and values from List<KeyValuePair<string, string>

private List
> KV_List = new List
>(); void initList() { KV_List.Add(new KeyValuePair
("qwer", "asdf")); KV_List.Add(new KeyValuePair
("qwer", "ghjk")); KV_List.Add(new KeyValuePair
("zxcv", "asdf")); KV_List.Add(new KeyValuePair
("hjkl", "uiop")); }
// #1: get all keys (remove Distinct() if you don't want it)List
allKeys = (from kvp in KV_List select kvp.Key).Distinct().ToList();// allKeys = { "qwer", "zxcv", "hjkl" }// #2: get values for a keystring key = "qwer";List
values = (from kvp in KV_List where kvp.Key == key select kvp.Value).ToList();// values = { "asdf", "ghjk" }// #3: get keys for a valuestring value = "asdf";List
keys = (from kvp in KV_List where kvp.Value == value select kvp.Key).ToList();// keys = { "qwer", "zxcv" }

https://stackoverflow.com/questions/31414429/c-sharp-get-keys-and-values-from-listkeyvaluepairstring-string

 

How to insert an item into a key/value pair object?

List
> kvpList = new List
>(){ new KeyValuePair
("Key1", "Value1"), new KeyValuePair
("Key2", "Value2"), new KeyValuePair
("Key3", "Value3"),};kvpList.Insert(0, new KeyValuePair
("New Key 1", "New Value 1"));
foreach (KeyValuePair
kvp in kvpList){ Console.WriteLine(string.Format("Key: {0} Value: {1}", kvp.Key, kvp.Value);}

 

转载于:https://www.cnblogs.com/shy1766IT/p/5313173.html

你可能感兴趣的文章
PAT——1060. 爱丁顿数
查看>>
分布式技术追踪 2017年第二十期
查看>>
git添加公钥后报错sign_and_send_pubkey: signing failed: agent refused operation的解决办法
查看>>
Linux环境变量永久设置方法(zsh)
查看>>
MVC4.0 利用IActionFilter实现简单的后台操作日志功能
查看>>
脑袋卡在窗子里
查看>>
ruby 中文字符to_json后乱码(unicode)
查看>>
《大道至简》第六章读后感
查看>>
codeforce 597C-Subsequences(dp+树状数组)
查看>>
[android](学习笔记6)为应用程序添加对话框(1)
查看>>
windows下mongodb安装与使用
查看>>
rotate the clock
查看>>
bugku 变量
查看>>
Python 环境傻瓜式搭建 :Anaconda概述
查看>>
数据库01 /Mysql初识以及基本命令操作
查看>>
数据库02 /MySQL基础数据类型以及多表之间建立联系
查看>>
Python并发编程04/多线程
查看>>
CF461B Appleman and Tree
查看>>
CF219D Choosing Capital for Treeland
查看>>
杂七杂八的小笔记本
查看>>