当前位置:   article > 正文

C#学习相关系列之数据类型---Diactionary字典的用法_c#字典 orderby

c#字典 orderby

一、字典的简介和常用方法

       在C#中,Dictionary<TKey, TValue>是一种非常常用的泛型集合类,用于存储键值对(Key-Value Pair)的数据结构。Dictionary<TKey, TValue>可以根据键快速查找对应的值,因此在需要快速查找和检索数据的场景下,特别是在涉及大量数据时,使用字典是非常高效的选择。本文将详细介绍Dictionary<TKey, TValue>的应用,包括创建字典、添加元素、访问元素、删除元素、遍历字典、常用的方法等内容。
常用方法:

  1. Comparer:           获取用于确定字典中的键是否相等的 IEqualityComparer
  2.   Count:                  获取包含在 Dictionary中的键/值对的数目。
  3.   Item:                    获取或设置与指定的键相关联的值。
  4.   Keys:                   获取包含 Dictionary中的键的集合。
  5.   Values:                获取包含 Dictionary中的值的集合。
  6.   Add:                    将指定的键和值添加到字典中。
  7.   Clear:                  从 Dictionary中移除所有的键和值。
  8.   ContainsKey:      确定 Dictionary是否包含指定的键。
  9.   ContainsValue:   确定 Dictionary是否包含特定值。             
  10.   GetEnumerator:  返回循环访问 Dictionary的枚举数。
  11.   GetType:             获取当前实例的 Type。 (从 Object 继承。)
  12.   Remove:             从 Dictionary中移除所指定的键的值。
  13.   ToString:             返回表示当前 Object的 String。 (从 Object 继承。)
  14.   TryGetValue:      获取与指定的键相关联的值。

二、主要方法方法简介

1、排序orderby

排序主要为正序orderby和反序OrderByDescending,二者用法相同

  1. public static Dictionary<int, string> dic = new Dictionary<int, string>();
  2. static void Main(string[] args)
  3. {
  4. dic.Add(1,"a");
  5. dic.Add(3, "c");
  6. dic.Add(6, "b");
  7. dic.Add(2, "f");
  8. 第一种方法:
  9. var tt = dic.OrderBy(x => x.Value);
  10. //orderby后面跟要排序的依据x为字典,通过x的value进行正向排序
  11. 第二种方法:
  12. // var tt = from s in dic orderby s.Value descending select s;
  13. foreach (var item in tt)
  14. {
  15. Console.WriteLine(item.Key+" "+item.Value);
  16. }

dictionary的排序主要用orderby方法,具体可以参考linq中orderby的用法。

2、对Dictionary求交集Intersect、差集Except、并集Union并集

  1. Dictionary<int, int> Dic1 = new Dictionary<int, int>();
  2. for (int i = 0; i < 10; i++)
  3. {
  4.   Dic1.Add(i, i);
  5. }
  6. Dictionary<int, int> Dic2 = new Dictionary<int, int>();
  7. for (int i = 5; i < 15; i++)
  8. {
  9.   Dic2.Add(i, i);
  10. }
  11. //求交集
  12. var jj = Dic1.Keys.Intersect(Dic2.Keys);
  13. foreach (var i in jj)
  14. {
  15.   Console.Write(i + " ");
  16. }
  17. //求差集
  18. var cj = Dic1.Keys.Except(Dic2.Keys);
  19. foreach (var i in cj)
  20. {
  21.   Console.Write(i + " ");
  22. }
  23. //求并集
  24. var bj = Dic1.Keys.Union(Dic2.Keys);
  25. foreach (var i in bj)
  26. {
  27.   Console.Write(i + " ");
  28. }

3、通过值查找键

  1. Dictionary<string, int> dict = new Dictionary<string, int>();
  2. dict.Add("apple", 1);
  3. dict.Add("banana", 2);
  4. dict.Add("orange", 3);
  5. string key = dict.FirstOrDefault(x => x.Value == 2).Key;
  6. //FirstOrDefault中跟的是判断条件
  7. Console.WriteLine(key); // 输出 "banana"

4.判断两个字典是否相等

  1. bool Equals(Dictionary<string, int> dict1, Dictionary<string, int> dict2)
  2. {
  3. var dict3 = dict2.Where(x => !dict1.ContainsKey(x.Key) || dict1[x.Key] != x.Value)
  4. .Union(dict1.Where(x => !dict2.ContainsKey(x.Key) || dict2[x.Key] != x.Value))
  5. .ToDictionary(x => x.Key, x => x.Value);
  6. return dict3.Count == 0;
  7. }

基础方法的使用:

【C# 基础精讲】字典(Dictionary)的使用_c# dictionary_繁依Fanyi的博客-CSDN博客

C#“字典”Dictionary的用法_c# dictionary_mr_five55的博客-CSDN博客

参考文献:

C#系 常用的LinQ查询表达式之orderby_yy763496668的博客-CSDN博客

C# 判断两个字典是否相等_c#2个字典 对比 方法-CSDN博客

声明:本文内容由网友自发贡献,转载请注明出处:【wpsshop】
推荐阅读
  

闽ICP备14008679号