![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
public
static
class
EnumerableExtension
{
/// <summary>
/// 将列表元素拼接成由splitter分隔的字符串
/// </summary>
/// <example>
/// 拼接字符串:
/// <c> new List<string> { "aa", "bb", "cc" }.Montage(p => p, ","); // 返回:"aa,bb,cc" </c>
/// 拼接对象属性:
/// <c> new List<string> { "aa", "bbb", "c" }.Montage(p => p.Length.ToString(), ","); // 返回:"2,3,1" </c>
/// 拼接枚举值:
/// <c> new List<DomainType> { DomainType.GuanHao, DomainType.YaoJiKe }.Montage(p => ((int)p).ToString(), ","); // 返回:"1,2" </c>
/// 拼接枚举名:
/// <c> new List<DomainType> { DomainType.GuanHao, DomainType.YaoJiKe }.Montage(p => p.ToString(), ","); // 返回:"GuanHao,YaoJiKe" </c>
/// </example>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="toString"> 将列表元素转换为字符串的委托 </param>
/// <param name="splitter"> 分隔符(可为空) </param>
/// <returns></returns>
public static string Montage < T > ( this IEnumerable < T > source, Func < T, string > toString, string splitter)
{
StringBuilder result = new StringBuilder();
splitter = splitter ?? string .Empty;
foreach (T item in source)
{
result.Append(toString(item));
result.Append(splitter);
}
string resultStr = result.ToString();
if (resultStr.EndsWith(splitter))
resultStr = resultStr.Remove(resultStr.Length - splitter.Length, splitter.Length);
return resultStr;
}
/// <summary>
/// 从泛型IEnumerable创建一个泛型List,每个元素由converter进行类型转换。
/// </summary>
/// <example>
/// 将枚举List转换为Int32 List:
/// <c> new DomainType[] { DomainType.GuanHao, DomainType.YaoJiKe }.ToList(p => (int)p); // 返回:List<int> </c>
/// </example>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="source"></param>
/// <param name="converter"></param>
/// <returns></returns>
public static List < TResult > ToList < TSource, TResult > ( this IEnumerable < TSource > source, Func < TSource, TResult > converter)
{
List < TResult > result = new List < TResult > ();
foreach (TSource item in source)
{
result.Add(converter(item));
}
return result;
}
}
{
/// <summary>
/// 将列表元素拼接成由splitter分隔的字符串
/// </summary>
/// <example>
/// 拼接字符串:
/// <c> new List<string> { "aa", "bb", "cc" }.Montage(p => p, ","); // 返回:"aa,bb,cc" </c>
/// 拼接对象属性:
/// <c> new List<string> { "aa", "bbb", "c" }.Montage(p => p.Length.ToString(), ","); // 返回:"2,3,1" </c>
/// 拼接枚举值:
/// <c> new List<DomainType> { DomainType.GuanHao, DomainType.YaoJiKe }.Montage(p => ((int)p).ToString(), ","); // 返回:"1,2" </c>
/// 拼接枚举名:
/// <c> new List<DomainType> { DomainType.GuanHao, DomainType.YaoJiKe }.Montage(p => p.ToString(), ","); // 返回:"GuanHao,YaoJiKe" </c>
/// </example>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="toString"> 将列表元素转换为字符串的委托 </param>
/// <param name="splitter"> 分隔符(可为空) </param>
/// <returns></returns>
public static string Montage < T > ( this IEnumerable < T > source, Func < T, string > toString, string splitter)
{
StringBuilder result = new StringBuilder();
splitter = splitter ?? string .Empty;
foreach (T item in source)
{
result.Append(toString(item));
result.Append(splitter);
}
string resultStr = result.ToString();
if (resultStr.EndsWith(splitter))
resultStr = resultStr.Remove(resultStr.Length - splitter.Length, splitter.Length);
return resultStr;
}
/// <summary>
/// 从泛型IEnumerable创建一个泛型List,每个元素由converter进行类型转换。
/// </summary>
/// <example>
/// 将枚举List转换为Int32 List:
/// <c> new DomainType[] { DomainType.GuanHao, DomainType.YaoJiKe }.ToList(p => (int)p); // 返回:List<int> </c>
/// </example>
/// <typeparam name="TSource"></typeparam>
/// <typeparam name="TResult"></typeparam>
/// <param name="source"></param>
/// <param name="converter"></param>
/// <returns></returns>
public static List < TResult > ToList < TSource, TResult > ( this IEnumerable < TSource > source, Func < TSource, TResult > converter)
{
List < TResult > result = new List < TResult > ();
foreach (TSource item in source)
{
result.Add(converter(item));
}
return result;
}
}