using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Bowin.Common.Utility { public static class EnumerableEx { public static void ForEach(this IEnumerable sources, Action action) { foreach (var item in sources) { action(item); } } public static void ForEach(this IEnumerable sources, Action action) { var index = 0; foreach (var item in sources) { action(item, index++); } } public static void ForEach(this IEnumerable sources, Func action) { foreach (var item in sources) { action(item); } } public static string ToStringEx(this IEnumerable enumerable, string separator) { return string.Join(separator, enumerable); } public static string ToStringEx(this IEnumerable enumerable, string separator) { return string.Join(separator, enumerable); } public static List MergeInt(this IEnumerable enumerable, string separator = "-") { return enumerable.Where(x => x.HasValue).Select(x => x.Value).ToList().MergeInt(separator); } public static List MergeInt(this IEnumerable enumerable, string separator = "-") { List result = new List(); var orderedEnumerable = enumerable.OrderBy(x => x); var numberCount = orderedEnumerable.Count(); if (numberCount == 0) return new List(); int firstNum = orderedEnumerable.First(); int lastNum = orderedEnumerable.First(); for (int i = 1; i < numberCount; i++) { var curNum = orderedEnumerable.ElementAt(i); if (curNum > (lastNum + 1)) { if (firstNum == lastNum) { result.Add(firstNum.ToString()); } else { result.Add(firstNum.ToString() + separator + lastNum.ToString()); } firstNum = curNum; } lastNum = curNum; } if (firstNum == lastNum) { result.Add(firstNum.ToString()); } else { result.Add(firstNum.ToString() + separator + lastNum.ToString()); } return result; } } }