keyvaluepair_Dictionary及KeyValuePair使用

///

///除去数组中的空值和签名参数并以字母a到z的顺序排序///

/// 过滤前的参数组

/// 过滤后的参数组

public static Dictionary FilterPara(SortedDictionarydicArrayPre)

{

Dictionary dicArray = new Dictionary();foreach (KeyValuePair temp indicArrayPre)

{if (temp.Key.ToLower() != "sign" && temp.Key.ToLower() != "sign_type" && temp.Value != "" && temp.Value != null)

{

dicArray.Add(temp.Key, temp.Value);

}

}returndicArray;

}

///

///把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串///

/// 需要拼接的数组

/// 拼接完成以后的字符串

public static string CreateLinkString(DictionarydicArray)

{

StringBuilder prestr= newStringBuilder();foreach (KeyValuePair temp indicArray)

{

prestr.Append(temp.Key+ "=" + temp.Value + "&");

}//去掉最後一個&字符

int nLen =prestr.Length;

prestr.Remove(nLen- 1, 1);returnprestr.ToString();

}

///

///把数组所有元素,按照“参数=参数值”的模式用“&”字符拼接成字符串,并对参数值做urlencode///

/// 需要拼接的数组

/// 字符编码

/// 拼接完成以后的字符串

public static string CreateLinkStringUrlencode(DictionarydicArray, Encoding code)

{

StringBuilder prestr= newStringBuilder();foreach (KeyValuePair temp indicArray)

{

prestr.Append(temp.Key+ "=" + HttpUtility.UrlEncode(temp.Value, code) + "&");

}//去掉最後一個&字符

int nLen =prestr.Length;

prestr.Remove(nLen- 1, 1);returnprestr.ToString();

}

C# get keys and values from List

private List> KV_List = new List>();voidinitList()

{

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 selectkvp.Key).Distinct().ToList();//allKeys = { "qwer", "zxcv", "hjkl" }//#2: get values for a key

string key = "qwer";

List values = (from kvp in KV_List where kvp.Key == key selectkvp.Value).ToList();//values = { "asdf", "ghjk" }//#3: get keys for a value

string value = "asdf";

List keys = (from kvp in KV_List where kvp.Value == value selectkvp.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 inkvpList)

{

Console.WriteLine(string.Format("Key: {0} Value: {1}", kvp.Key, kvp.Value);

}

Dictionary dicShiftDailys = new Dictionary();

dicShiftDailys = (from entry in dicShiftDailys

orderby entry.Key ascending

select entry).ToDictionary(pair => pair.Key, pair => pair.Value)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值