You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
BookingHeChuan/Myshipping.Core/Extension/DictionaryExtensions.cs

49 lines
1.3 KiB
C#

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