日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

SortedDictionaryTKey,TValue正序与反序排序及Dicttionary相关

發布時間:2023/12/18 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SortedDictionaryTKey,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+"\r\n");}}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("\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())

轉載于:https://www.cnblogs.com/shy1766IT/p/5046389.html

總結

以上是生活随笔為你收集整理的SortedDictionaryTKey,TValue正序与反序排序及Dicttionary相关的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。