using System.Collections.Generic; using System.Linq; using System.Text; namespace Myshipping.Core; /// /// 字典扩展 /// public static class DictionaryExtensions { /// /// 将一个字典转化为 QueryString /// /// /// /// public static string ToQueryString(this Dictionary dict, bool urlEncode = true) { return string.Join("&", dict.Select(p => $"{(urlEncode ? p.Key?.UrlEncode() : "")}={(urlEncode ? p.Value?.UrlEncode() : "")}")); } /// /// 将一个字符串 URL 编码 /// /// /// public static string UrlEncode(this string str) { if (string.IsNullOrEmpty(str)) { return ""; } return System.Web.HttpUtility.UrlEncode(str, Encoding.UTF8); } /// /// 移除空值项 /// /// public static void RemoveEmptyValueItems(this Dictionary dict) { dict.Where(item => string.IsNullOrEmpty(item.Value)).Select(item => item.Key).ToList().ForEach(key => { dict.Remove(key); }); } }