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("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())
public class RDfsedfw { /// <summary> /// 将匿名类转换为字典 /// </summary> /// <returns></returns> public Dictionary<string,object> ClassToDic() { var data = new { action = "GetProperties", ip ="192.168.1.150", encryp = 0, date = DateTime.Now, model = "cash", sn = "2222", vs="" }; var d = data.GetType().GetProperties()//这一步获取匿名类的公共属性,返回一个数组 .OrderBy(q => q.Name)//这一步排序,需要引入System.Linq,当然可以省略 .Where(q => q.Name!="vs")//这一步筛选,也可以省略 .ToDictionary(q => q.Name, q => q.GetValue(data));//这一步将数组转换为字典 return d; } public Dictionary<string,object> ClassToDic1() { DataModel dm1 = new DataModel() { Str = "abc123", Date = DateTime.Now, Chensf = 222, Dic = new Dictionary<string, string>() { { "ID", "1" }, { "name", "mike" } }, Gname = "fffffs"//这个字段由于不是属性,是不能被GetProperties转换为数组元素的 }; var d = dm1.GetType().GetProperties() .ToDictionary(q=>q.Name,q=>q.GetValue(dm1)); return d; } } public class DataModel { public string Str { set; get; } public DateTime Date { set; get; } public int Chensf { set; get; } public Dictionary<string ,string> Dic { set; get; } public string Gname; }
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。

浙公网安备 33010602011771号