Code
Public static class EnumerableExtension
{
/// <Summary>
/// Concatenates the list elements into strings separated by splitter
/// </Summary>
/// <Example>
/// Concatenated string:
/// <C> new List & lt; string & gt; {"aa", "bb", "cc "}. montage (p => p, ","); // return value: "aa, bb, cc" </c>
/// Attributes of the spliced object:
/// <C> new List & lt; string & gt; {"aa", "bbb", "c "}. montage (p => p. length. toString (), ","); // return: "2, 3, 1" </c>
/// Spliced enumerated values:
/// <C> new List & lt; DomainType & gt; {DomainType. guanHao, DomainType. yaoJiKe }. montage (p => (int) p ). toString (), ","); // return value: "1, 2" </c>
/// Splicing enumeration Name:
/// <C> new List & lt; DomainType & gt; {DomainType. guanHao, DomainType. yaoJiKe }. montage (p => p. toString (), ","); // return value: "GuanHao, YaoJiKe" </c>
/// </Example>
/// <Typeparam name = "T"> </typeparam>
/// <Param name = "source"> </param>
/// <Param name = "toString"> delegate a list element to a string </param>
/// <Param name = "splitter"> separator (can be empty) </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>
/// Create a generic List from the generic IEnumerable, and each element is converted by converter.
/// </Summary>
/// <Example>
/// Convert the enumerated List to Int32 List:
/// <C> new DomainType [] {DomainType. guanHao, DomainType. yaoJiKe }. toList (p => (int) p); // return: List & lt; int & gt; </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;
}
}