List<int> 转 string []

List<int> 转 string []

http://blog.sina.com.cn/s/blog_54b14bb80100xvl1.html

今天有同事问这个问题,就是说,存在一个list<int>: 1,2,3,4,5,6,7

希望可以转换成字符串:“1,2,3,4,5,6,7”

 

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using test2;

namespace test

{

class Program

{

static void Main(string[] args)

{

List<int> agreementIds = new List<int>() { 1, 2, 3, 4, 5, 6, 7 };

//Solution1

foreach (int i in agreementIds)

Console.WriteLine(i);

//Solution2

Console.WriteLine(String.Join(",",agreementIds.ConvertAll((i)=>i.ToString()).ToArray()));

//Solution3

Console.WriteLine(String.Join(",", agreementIds.ConvertToString(1)));

////Solution4

Func<List<int>, string[]> ToArray = p1 => p1.ConvertToString(1);

Func<List<int>, string[]> ToArray1 = p1 => p1.ConvertAll((i) => i.ToString()).ToArray();

Func<List<int>, string[]> ToArray2 = delegate(List<int> x) { return x.ConvertAll((i) => i.ToString()).ToArray(); };

Console.WriteLine(string.Join(",", ToArray(agreementIds)));

Console.WriteLine(string.Join(",", ToArray1(agreementIds)));

Console.WriteLine(string.Join(",", ToArray2(agreementIds)));

Console.ReadLine();

}

}

}

//Solution3

namespace test2

{

public static class MyExtensions

{

public static string[] ConvertToString(this List<int> s,int a)

{

return s.ConvertAll((i) => i.ToString()).ToArray();

}

}

}

运行结果如下 :

List<int> <wbr>转 <wbr>string <wbr>[]

 

posted @ 2013-05-14 16:59  蓝晓  阅读(757)  评论(0编辑  收藏  举报