JsHelper.cs 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Common.Helper
  7. {
  8. public class JsHelper
  9. {
  10. /// <summary>
  11. /// 计算使用最多的数字
  12. /// </summary>
  13. /// <param name="list">字符串集合</param>
  14. /// <param name="number">需要几位</param>
  15. /// <returns></returns>
  16. public static IEnumerable<string> UseMax(List<string> list, int number = 3)
  17. {
  18. list = list.Where(p => !string.IsNullOrEmpty(p)).ToList();
  19. var dict = new Dictionary<string, int>();
  20. var group = list.GroupBy(p => p).ToList();
  21. group.ForEach(p =>
  22. {
  23. dict.Add(p.Key, list.Where(q => q == p.Key).Count());
  24. });
  25. var dictOrder = dict.OrderByDescending(p => p.Value);
  26. if (list.Count > 0)
  27. {
  28. number = dictOrder.Count() > number ? number : dictOrder.Count();
  29. var i = 1;
  30. foreach (var item in dictOrder)
  31. {
  32. if (i <= number)
  33. yield return item.Key;
  34. i++;
  35. }
  36. }
  37. }
  38. /// <summary>
  39. /// 计算使用最多的数字
  40. /// </summary>
  41. /// <param name="list">字符串集合</param>
  42. /// <param name="number">需要几位</param>
  43. /// <returns></returns>
  44. public static string UseMaxString(List<string> list, int number = 3)
  45. {
  46. return UseMax(list, number).Join();
  47. }
  48. }
  49. }