C#工具类之字典扩展类

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace G2.Utils
{
    /// <summary>
    /// 字典帮助类
    /// </summary>
    public static class DictionaryHelper
    {
        /// <summary>
        /// 字典排序(默认排序)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">无序字典</param>
        /// <returns>有序字典</returns>
        public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("字典为空,不能排序");
            }

            return new SortedDictionary<TKey, TValue>(dictionary);
        }

        /// <summary>
        /// 字典排序(自定义排序)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">无序字典</param>
        /// <param name="comparer">自定义排序方法</param>
        /// <returns>有序字典</returns>
        public static IDictionary<TKey, TValue> Sort<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TKey> comparer)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("字典为空,不能排序");
            }

            if (comparer == null)
            {
                throw new ArgumentNullException("自定义排序方法为空,不能排序");
            }

            return new SortedDictionary<TKey, TValue>(dictionary, comparer);
        }

        /// <summary>
        /// 字典排序(依据值默认排序)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">无序字典</param>
        /// <returns>有序字典</returns>
        public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
        {
            return dictionary.OrderBy(v => v.Value).ToDictionary(item => item.Key, item => item.Value);
        }

        /// <summary>
        /// 字典排序(依据值自定义排序)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">无序字典</param>
        /// <param name="comparer">自定义排序方法</param>
        /// <returns>有序字典</returns>
        public static IDictionary<TKey, TValue> SortByValue<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, IComparer<TValue> comparer)
        {
            return dictionary.OrderBy(v => v.Value, comparer).ToDictionary(item => item.Key, item => item.Value);
        }

        /// <summary>
        /// 判断字典是否为空或者无数据
        /// </summary>
        /// <param name="dictionary">字典</param>
        /// <returns>True:字典为空或没有数据;否则为False</returns>
        public static bool IsEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("字典为空");
            }

            return dictionary.Count == 0;
        }

        /// <summary>
        /// 判断字典是否为空或者无数据
        /// </summary>
        /// <param name="dictionary">字典</param>
        /// <returns>True:字典为空或没有数据;否则为False</returns>
        public static bool IsNullOrEmpty<TKey, TValue>(this IDictionary<TKey, TValue> dictionary)
        {
            return dictionary == null || dictionary.Count == 0;
        }

        /// <summary>
        /// 获取字典值(如果不包含该值则由自定义方法返回)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">字典</param>
        /// <param name="key">Key值</param>
        /// <param name="function">自定义方法</param>
        /// <returns>字典里的值或者自定义方法返回值</returns>
        public static TValue GetOrGetByFunction<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, Func<TValue> function)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("字典为空");
            }

            if (dictionary.ContainsKey(key))
            {
                return dictionary[key];
            }

            if (function == null)
            {
                throw new ArgumentNullException("自定义取值方法为空,不能获取值");
            }

            return function();
        }

        /// <summary>
        /// 新增或重写(如果没有该Key值,则新增;否则重写该值)
        /// </summary>
        /// <typeparam name="TKey">Key类型</typeparam>
        /// <typeparam name="TValue">Value类型</typeparam>
        /// <param name="dictionary">字典</param>
        /// <param name="key">Key值</param>
        /// <param name="value">Value新值</param>
        /// <returns>字典</returns>
        public static IDictionary<TKey, TValue> AddOrSet<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue value)
        {
            if (dictionary == null)
            {
                throw new ArgumentNullException("字典为空");
            }

            if (dictionary.ContainsKey(key))
            {
                dictionary[key] = value;
            }
            else
            {
                dictionary.Add(new KeyValuePair<TKey, TValue>(key, value));
            }

            return dictionary;
        }
    }
}

 

转载于:https://www.cnblogs.com/zhshlimi/p/6667511.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值